在Google Plus API中,GTMOAuth2ViewControllerTouch返回nil虽然使用了正确的clientId,clientSecret,访问令牌

时间:2013-06-07 09:34:55

标签: ios xcode authentication google-plus google-authentication

在iOS应用中,我使用以下代码在另一个用户已使用google plus登录API登录的视图控制器中检索用户身份验证

GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc]
                                                 initWithScope:@"https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me"
                                                 clientID:CLIENT_ID
                                                 clientSecret:CLIENT_SECRET
                                                 keychainItemName:NAME
                                                 delegate:self
                                                 finishedSelector:@selector(viewController:finishedWithAuth:error:)];
[[self navigationController] pushViewController:_googlePlusCtrl animated:YES];

但是当我在代码下运行时,viewController.authentication.accessToken给出nil值,viewController.authentication也给出nil值。

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error 
{
    if (error != nil) {
        // Authentication failed
        NSLog(@"failed");
    } else {
        // Authentication succeeded
        NSLog(@"Success");
    }
}
编辑:回应阿诗的评论......

我在app delegate中有这个:

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [GPPURLHandler handleURL:url 
                  sourceApplication:sourceApplication 
                         annotation:annotation];
}

我是否必须在此功能中编写任何特定代码?

1 个答案:

答案 0 :(得分:0)

我拥有的代码(以及this SO Question中的代码)仅使用https://www.googleapis.com/auth/plus.me作为范围。我的代码在大多数地方(包括你的appDelegate实现)与你的代码差不多,但我的google +按钮IBAction的范围不同,如下所示(,为了这个问题的目的,忽略使用中的差异)界面习惯用法和viewController转换的类型;它与答案无关,只是说明它是如何以不同的方式用于接近相同的效果):

    NSString *scope = @"https://www.googleapis.com/auth/plus.me"; // scope for Google+ API
    SEL gPlusSignInFinished = @selector(googlePlusSignInViewController:finishedWithAuth:error:);
    GTMOAuth2ViewControllerTouch *gtmOAuth2ViewController
      = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                 clientID:GPLUS_CLIENT_ID
                                             clientSecret:GPLUS_CLIENT_SECRET
                                         keychainItemName:kKeychainItemName
                                                 delegate:self
                                         finishedSelector:gPlusSignInFinished];

    // note that the controller is passed in both situations for differing idioms
    if (UIUserInterfaceIdiomPhone == UIDevice.currentDevice.userInterfaceIdiom)
        [self presentModalViewController:gtmOAuth2ViewController animated:YES];
    else
        [self gPlusPopover:gtmOAuth2ViewController];