2012-02-10 4 views
7

私は、彼が関連付けられているGoogleアカウントを使用するためにユーザーを認証することに取り組んでいます。問題は、ユーザーが自分のアプリからログインするたびに、前回のテストから既にアクセスを許可していても、Googleの認証ビューに「アクセス許可」が常に表示されることです。これは正常ですか、私のコードを間違っていますか?私を助けてください。iOS:Google認証コード

私はアウトにlogginため、次のコードを使用:

- (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

私はクライアントID、clientSecret、keychainItemNameはcorrect.Canですが、あなたがここで間違っているものをexplian「GTMOAuth2ViewControllerTouch *のViewController」でゼロ値を得ましたか。 – Ponting

答えて

-2

私の経験上、この動作は正常です。

Facebookのユーザーに、ユーザーのプロフィールにアクセスするためのアプリ権限を許可する必要があるかどうかを尋ねるだけなので、疑問がありますか?ドキュメントから

+0

これはまったく意味がありません。アンドロイドでさえDrive SDKは、ユーザーがいつ権限を既に与えているのか尋ねるだけで動作します。 GoogleがiOSのバージョンを異ならせる理由は何ですか? – KVISH

3

キーチェーン項目名は、ユーザのキーチェーンにトークンを保存するために使用され、アプリケーション名およびサービス名(複数可)の両方を特定する必要があります。 keychainItemNameがnilの場合、トークンは保存されず、ユーザーは次にアプリケーションを実行するときに再度サインインする必要があります。

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

だから、あなたのコードから、それはkKeychainItemNameに設定されているものに依存します。

私はドキュメントを読んでいたので、これについてコメントしたいと思っていました。

0

これは古い質問ですが、同じ問題が発生したので、私は自分の解決策を書いているので、将来他の人に役立つかもしれません。

あなたもself.analyticsService.authorizer変数

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

これは私のためにトリックをしたを設定する必要があり、ユーザーはノーである、それだけでself.authを設定するには十分ではありませんが判明より長い資格情報を入力するように頼んだ。

3

使用あなただけチェックして、この

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch 
             authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME 
             clientID:GOOGLE_CLIENT_KEY 
             clientSecret:GOOGLE_CLIENT_SECRET]; 
を使用してのOAuthオブジェクトを取得

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth]; 

APIへの呼び出しを行う前に、キーチェーンに保存するためのOAuthオブジェクトを取得し、この方法

とそれを使用してoauthオブジェクトが本物であることを確認してください

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

鍵チェーン名とクライアント秘密IDを取得する方法を教えてください。私はGoogleアカウントで作成しているときにのみ、クライアントIDを取得することができます。 – Alok

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]; 

] 
関連する問題