2013-07-14 39 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可能是有用的。它解釋瞭如何使用身份驗證令牌以及在用戶重新啓動應用程序時如何處理鑰匙串。