【问题标题】:Google OAuth error -1001谷歌 OAuth 错误 -1001
【发布时间】:2016-02-05 22:25:30
【问题描述】:

在我的应用程序中,我尝试实现谷歌帐户访问,当我初始化它的工作直到登录会话时。之后它会在屏幕截图中引发以下错误

这是我的代码 初始化和方法实现

 static NSString *const kKeychainItemName =nil;
 NSString *kMyClientID = @"465568347336.apps.googleusercontent.com";     
 NSString *kMyClientSecret = @"rKVsWXTlo3M8zqNfofkX0Xrl"; 
 NSString *scope = @"https://www.googleapis.com/auth/userinfo.profile"; 

 GTMOAuth2ViewControllerTouch *viewController;
 viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                                            clientID:kMyClientID
                                                        clientSecret:kMyClientSecret
                                                    keychainItemName:kKeychainItemName
                                                            delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)];
 [self.navigationController presentModalViewController:viewController animated:YES];

错误处理程序

 - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
  finishedWithAuth:(GTMOAuth2Authentication *)auth
             error:(NSError *)error {
 if (error != nil) {
    NSString *output=nil;
    output = [error description];
    NSLog(@"output:%@",output);
    UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                   message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:@"Try again", nil];
    fail.tag = 1;

    [fail show];
    NSLog(@"Authentication failed!");
  } else {
    UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                      message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    success.tag = 2;

    [success show];
    NSLog(@"Autzentication succeeded!");
   }

如何解决这个问题。请帮我解决

【问题讨论】:

    标签: iphone ios google-oauth


    【解决方案1】:

    我使用以下代码实现了我的 GTMOAuth2,它对我有用,我希望它能以某种方式帮助你。

    - (GTMOAuth2Authentication * )authForGoogle
    {    
        NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL];
    
        NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob";
    
        _auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"Google"
                                                             tokenURL:tokenURL
                                                          redirectURI:redirectURI
                                                             clientID:GoogleClientID
                                                         clientSecret:GoogleClientSecret];
        _auth.scope = @"https://www.googleapis.com/auth/userinfo.profile";
        return _auth;
    }
    
    
    - (void)signInToGoogle
    {   
        _auth = [self authForGoogle];
    
        // Display the authentication view
        GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:_auth
                                                                                                     authorizationURL:[NSURL URLWithString:GoogleAuthURL]
                                                                                                keychainItemName:@"GoogleKeychainName"
                                                                                                        delegate:self
                                                                                                finishedSelector:@selector(viewController:finishedWithAuth:error:)];
        [_window setRootViewController: viewController];
        [_window makeKeyAndVisible];
    }
    
     - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth
             error:(NSError *)error 
    {
        if (error != nil) {
           NSString *output=nil;
           output = [error description];
           NSLog(@"output:%@",output);
           UIAlertView *fail = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                   message:[NSString stringWithFormat:@"Error, Authentication failed!\n %@",error]
                                                  delegate:self
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:@"Try again", nil];
           fail.tag = 1;
    
           [fail show];
           NSLog(@"Authentication failed!");
        } else {
           UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                      message:[NSString stringWithFormat:@"Authentication succeeded!"]
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
           success.tag = 2;
    
           [success show];
           NSLog(@"Autzentication succeeded!");
        }
    }
    

    我复制了您的finishedWithAuth 方法,因为它与我的实现类似。我认为我们的代码在 xcode 中实现 GTMOAuth2 方面没有太大区别,但我在使用 GTMOAuth2 时意识到的一件事是,调试您面临的任何错误都非常困难。我也遇到了类似的错误,并意识到这是因为我在设置应用程序并获取 clientID 和 clientSecret 时在 Google 门户中选择了错误的应用程序类型。我一开始将它设置为 iOS 应用程序(当然!),但在网上阅读了不同的答案和问题后,我意识到我应该将它创建为其他应用程序类型。这解决了我的问题。也许你可以去看看。

    他们有一个支持非常全面的论坛,这是链接here

    为了进一步补充,我可以向您推荐我在将 GTMOAuth2 集成到我的应用程序时参考的教程。这是链接here

    另外,由于我正在开发一个需要检查用户电子邮件地址的企业应用程序,即使在用户通过身份验证后,我也很难收到电子邮件。在我得到我需要的东西之前,我必须破解并阅读代码,如果你将来需要它,你可以查看我的答案here on SO

    希望这会有所帮助! :)

    【讨论】:

      【解决方案2】:

      Error Domain=com.google.GTMOAuth2 Code=-1001 通常在用户登录时发生,但在 OAuth 协议窗口中(例如“想要访问您的电子邮件”)用户点击“取消”或“不,谢谢”。

      所以基本上没有办法“解决”这个问题。您可以处理它,或者在没有该服务的情况下仅在您的应用程序中取得进展。

      【讨论】:

        猜你喜欢
        • 2016-10-09
        • 2013-07-04
        • 2017-08-15
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-10
        相关资源
        最近更新 更多