iOS:Googleauthentication码

我正在validation用户身份,以使用他所关联的Google帐户。 问题是,每当用户通过我的应用程序login,“允许访问”总是出现在谷歌的身份validation视图,即使我点击了从以前的testing已经允许访问。 这是正常的,还是我做我的代码错了? 请帮帮我吧。

我在下面的代码中使用了下面的代码:

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

从文档:

钥匙串项目名称用于将标记保存在用户的钥匙串中,并应标识您的应用程序名称和服务名称。 如果keychainItemName为零,则令牌不会被保存,用户下次运行应用程序时将不得不再次login。

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

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

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

当你得到oauth对象保存到钥匙串中时使用这个方法

 [GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth]; 

在调用API之前,只需使用它检查和检索oauth对象

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

并确保它使用这个oauth对象是可信的

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

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

原来只设置self.auth是不够的,你还需要设置self.analyticsService.authorizervariables

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

这为我做了诡计,用户不再被要求input凭据。

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

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

你有疑问,因为脸书只向用户提出一次,如果用户想要授予应用程序的权限来访问用户的个人资料?