IOS: Google Authentication Code

I am working with user authentication to use the google account with which it is associated. The problem is that every time a user logs in through my application, “Allow access” always appears in the Google authentication view, even if I clicked “Allow access” from the previous test. Is this normal, or am I doing my codes wrong? Please help me guys.

I used the following codes for loggin in out:

- (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."]; } 

This is for the delegate:

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

And 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; } 

By the way, my link for these codes is located at this link: http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers

+7
source share
5 answers

from documents:

The keychain item name is used to store the token in the user keychain and should identify both your application name and the service name. If keychainItemName is nil, the token will not be saved, and the user will have to log in again the next time the application starts.

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

So, from your code, it depends on what kKeychainItemName is installed for.

I just thought that I would comment on this when I read the documents.

+3
source

Use this method when you get an oauth object to save to the keychain

 [GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth]; 

and

before calling api just check and extract the oauth object using this

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

and make sure the oauth object is genuine using this

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

I know this is an old question, but I ran into the same problem, so I am writing my solution, this may help someone else in the future.

It turns out that this is not enough to set only self.auth,, you also need to set the variable self.analyticsService.authorizer

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

This did the trick for me, the user no longer needs to enter credentials.

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

From my experience, this behavior is normal.

Do you have doubts because facebook only asks the user once if the user wants to grant application privileges to access the user profile?

-2
source

All Articles