2013-07-14 38 views
1

我一直在Google的OAuth2 API提供Objective-C库中。Google OAuth2记住用户?

无论如何,我有麻烦了解如何让应用程序记住谁登录。我已成功通过使用以下身份验证工作。

我调用以下方法来呈现来自Google的OAuth视图控制器,用户登录并进行身份验证。但是,每次我重新启动应用程序时,它都会再次运行登录屏幕,就好像我没有通过身份验证一样。据我了解,我需要有一些钥匙串/令牌记忆,以便识别最近登录的用户。

但是如何?我无法从可用的小文档中找到它,所以我们将不胜感激,谢谢。

-(void)authenticateUserFromViewController:(UIViewController *)viewController 
{ 
    GTMOAuth2ViewControllerTouch *authViewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeYouTube 
                   clientID:kClientID 
                  clientSecret:kClientSecret 
                 keychainItemName:kKeychainItemName 
                   delegate:self 
                 finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 

    [viewController.navigationController pushViewController:authViewController animated:YES]; 
} 

-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error 
{ 
    if (error != nil) 
    { 
     // Authentication failed 
     NSLog(@"Failed to authorise"); 
    } 
    else 
    { 
     // Authentication succeeded 
     NSLog(@"Authorised"); 
    } 
} 
+0

我对Objective-C库不熟悉,但可能[本教程](http://devcenter.kinvey.com/ios/tutorials/ios-oauth2-tutorial)可能会有用。它说你需要调用[GTMOAuth2Authentication accessToken],这似乎就是你想要的。有关OAuth 2.0流程的更多信息:https://developers.google.com/youtube/v3/guides/authentication –

+0

有关此工作示例,请查看他们的[github](https://github.com/KinveyApps/) Kinvey-的OAuth2-DI-样本)。根据你的代码测试他们的代码,看看有什么可行,哪些不可行。 – Enigmadan

+0

关于钥匙串,还有[Google参考资料](http://code.google.com/p/gtm-oauth2/wiki/Introduction#Retrieving_Authorization_from_the_Keychain)中的内容。 –

回答

1

我不熟悉的Objective-C库,但也许this part of the Google Reference可能是有用的。它解释了如何使用身份验证令牌以及在用户重新启动应用程序时如何处理钥匙串。