iOS:Google身份验证代码

问题描述:

我正在验证用户身份以使用他与之关联的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

从我的经验来看,这种行为是正常的。

您是否怀疑是因为如果用户想要授予应用程序访问用户个人资料的权限,Facebook只会向用户提问一次?

+0

这绝对没有意义,即使在Android上,Drive SDK的工作原理也只是询问用户何时__NOT__已经提供了权限。 Google为什么要让iOS版本不同? – KVISH 2015-10-07 04:54:19

从文档:

钥匙串项目名称是用来保存用户的钥匙链令牌,并应同时识别您的应用程序名称和服务名称(S)。如果keychainItemName为零,则不会保存令牌,并且用户将在下次运行应用程序时再次登录。

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

所以,从你的代码,这取决于什么kKeychainItemName设置为。

只是觉得我会在阅读文档时对此进行评论。

我知道这是一个古老的问题,但我遇到了同样的问题,所以我正在写我的解决方案,它可能会在未来帮助其他人。

原来这是不够的,只设置self.auth,你还需要设置self.analyticsService.authorizer变量

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

这并获得成功对我来说,用户是没有更长的时间要求输入凭证。

使用此方法时,你得到的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

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

]