2012-02-10 136 views
7

我正在驗證用戶身份以使用他與之關聯的Google帳戶。問題在於,每當用戶通過我的應用程序登錄時,即使我點擊了先前測試中的「允許訪問」,Google的身份驗證視圖中也始終顯示「允許訪問」。這是正常的還是我在做我的代碼錯誤?請幫助我們。iOS:Google身份驗證代碼

我用下面的代碼洛在出:

- (IBAction)signIn:(id)sender { 
    if(!isSignedIn){ 
     [self signOutFromAll]; 

     NSString *keychainItemName = nil; 

     // save keychain 
     keychainItemName = kKeychainItemName; 

     NSString *scope = @"https://www.googleapis.com/auth/plus.me"; 

     NSString *clientID = kClientID; 
     NSString *clientSecret = kClientSecret; 

     SEL finishedSel = @selector(viewController:finishedWithAuth:error:); 

     GTMOAuth2ViewControllerTouch *viewController; 
     viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope 
                    clientID:clientID 
                   clientSecret:clientSecret 
                  keychainItemName:keychainItemName 
                    delegate:self 
                  finishedSelector:finishedSel]; 

     [[self navigationController]pushViewController:viewController animated:YES]; 
    } else { 
     [self displayAlertWithMessage:@"Currently Signed in."]; 
    } } 

- (IBAction)signOut:(id)sender { 
    [self signOutFromAll]; 
    [self displayAlertWithMessage:@"Signed out."]; } 

這是代表:

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
     finishedWithAuth:(GTMOAuth2Authentication *)auth 
       error:(NSError *)error{ 
    if(error != nil){ 
     // Authentication failed... 
     NSLog(@"Authentication error: %@", error); 
     NSData *responseData = [[error userInfo] objectForKey:@"data"]; 
     if([responseData length] > 0) 
      NSLog(@"%@", [[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]autorelease]); 
     self.auth = nil; 
    } else { 
     // Authentication succeeded... 
     isSignedIn = YES; 
     self.auth = auth; 
    } 
} 

而且awakeFromNib:

- (void)awakeFromNib{ 
    // Fill in the Client ID and Client Secret text fields 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    // First, we'll try to get the saved Google authentication, if any, from the keychain 
    // Normal applications will hardcode in their client ID and client secret, 
    // But the sample app allows the user to enter them in a text field, and saves them in the preferences 
    NSString *clientID  = [defaults stringForKey:kGoogleClientIDKey]; 
    NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey]; 

    GTMOAuth2Authentication *auth; 

    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName 
                   clientID:clientID 
                  clientSecret:clientSecret]; 

    if (auth.canAuthorize) { 
     // There is saved google authentication 
     // self.serviceSegments.selectedSegmentIndex = 0; 
    } 

    // Save the authentication object, which holds the auth tokens 
    self.auth = auth; 

    [self setAuth:auth]; 
    isSignedIn = self.auth.canAuthorize; 
} 

通過我參考的方式對於這些代碼是在這個鏈接:http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers

+0

雖然clientID,clientSecret,keychainItemName是正確的,但我得到了「GTMOAuth2ViewControllerTouch * viewController」中的nil值。你可以解釋這裏有什麼問題嗎? – Ponting 2013-06-17 11:31:25

回答

-2

從我的經驗來看,這種行爲是正常的。

您是否懷疑是因爲如果用戶想要授予應用程序訪問用戶個人資料的權限,Facebook只會向用戶提問一次?

+0

這絕對沒有意義,即使在Android上,Drive SDK的工作原理也只是詢問用戶何時__NOT__已經提供了權限。 Google爲什麼要讓iOS版本不同? – KVISH 2015-10-07 04:54:19

3

從文檔:

鑰匙串項目名稱是用來保存用戶的鑰匙鏈令牌,並應同時識別您的應用程序名稱和服務名稱(S)。如果keychainItemName爲零,則不會保存令牌,並且用戶將在下次運行應用程序時再次登錄。

http://code.google.com/p/gtm-oauth2/wiki/Introduction

所以,從你的代碼,這取決於什麼kKeychainItemName設置爲。

只是覺得我會在閱讀文檔時對此進行評論。

0

我知道這是一個古老的問題,但我遇到了同樣的問題,所以我正在寫我的解決方案,它可能會在未來幫助其他人。

原來這是不夠的,只設置self.auth,你還需要設置self.analyticsService.authorizer變量

if ([self.auth canAuthorize]) 
{ 
    self.analyticsService.authorizer = self.auth; 
    [self getAnalyticsData]; 
    return; 
} 

這並獲得成功對我來說,用戶是沒有更長的時間要求輸入憑證。

3

使用此方法時,你得到的OAuth對象保存到鑰匙扣

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth]; 

製作到API的調用之前只是檢查和使用該

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch 
             authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME 
             clientID:GOOGLE_CLIENT_KEY 
             clientSecret:GOOGLE_CLIENT_SECRET]; 
檢索OAuth的對象

並確保它的oauth對象是真實的,使用這個

if(![GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth]) 
+0

可否請你建議我如何獲得鑰匙鏈名稱和客戶端密碼。當我使用Google帳戶創建時,只能獲取客戶端ID。 – Alok 2017-01-11 07:18:28

0
Put the below code to logout/sign out from Google SDK. 

- Call below function from where you want: 

static NSString *const kKeychainItemName = @"MY_APP"; 



- (void)logoutFromGoogleDrive { 

[GTMOAuth2SignIn revokeTokenForGoogleAuthentication:(GTMOAuth2Authentication *)self.driveService.authorizer]; 

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:kKeychainItemName authentication:nil]; 

} 

[Note: Above code works, if you have used GTMOAuth2SignIn for sign in user for google access like, 

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch 
authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME 
clientID:GOOGLE_CLIENT_KEY 
clientSecret:GOOGLE_CLIENT_SECRET]; 

]