【问题标题】:In Google Plus API,GTMOAuth2ViewControllerTouch returning nil although using right clientId,clientSecret,access token在 Google Plus API 中,GTMOAuth2ViewControllerTouch 使用正确的 clientId、clientSecret、访问令牌返回 nil
【发布时间】:2013-06-03 13:37:57
【问题描述】:

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

编辑:回应 Ashim 的评论 ...

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

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

我必须在这个函数中编写任何特定的代码吗?

【问题讨论】:

  • 你在你的 AppDelegate 中实现了方法 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 吗?
  • 我在应用程序委托中有这个: - (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation { return [GPPURLHandler handleURL:url sourceApplication:sourceApplication 注解:注解];我必须在这个函数中编写任何特定的代码吗?
  • 这是一个小问题,但您可能应该使用googleapis.com/auth/plus.login 范围。
  • @class : 我会试试然后回复你。
  • @class :它不起作用。

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


【解决方案1】:

我拥有的代码(以及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];

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-01
  • 2012-04-15
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 2014-05-28
相关资源
最近更新 更多