1

在 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");
    }
}

编辑:回应阿希姆的评论......

我在应用程序委托中有这个:

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

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

4

1 回答 1

0

我拥有的代码(以及此 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];
于 2013-06-16T19:03:41.027 回答