1

GTMOAuth 2.0 seems like an excellent tool for OAuth 2.0 verification on iOS. I am trying to retrieve the full name and email of a Google user by implementing GTMOAuth-2 in Xcode but am having a bit of trouble. Based on this answer: Retrieve User email using GTM OAuth2 for iOS, it should be as easy as calling auth.userEmail. However, the problem is that calling auth.userEmail in the following code segment always return null:

- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController
  finishedWithAuth:(GTMOAuth2Authentication * )auth
             error:(NSError * )error
{
NSLog(@"finished");
NSLog(@"auth access token: %@", auth.accessToken);

[self.navigationController popToViewController:self animated:NO];
if (error != nil) {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google"
                                                     message:[error localizedDescription]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
    [alert show];
} else {

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success Authorizing with Google"
                                                     message:[error localizedDescription]
                                                    delegate:nil
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
    [alert show];
}
NSLog(@"email: %@",auth.userEmail);

}

The code runs successfully and retrieves an access token, but auth.userEmail is always null. Do I need to make a request to the Google email endpoint using GTMOAuth 2.0's Fetcher object, or otherwise send an additional HTTP GET request to retrieve the user's email using auth.accessToken?

Community
  • 1
  • 1

1 Answers1

0

I recently worked on Google OAuth2 for logging user in with gmail by following tutsplus's tutorial and It gave me the desired results.I would recommend you to follow this link. This provides methods to login and logout and also email address of logged in user. Google OAuth2 . And to get email address of logged in user, add this in scopes https://www.googleapis.com/auth/userinfo.email. and code will look like this

 [_googleOAuth authorizeUserWithClienID:@"YOUR CLIENT ID"
                           andClientSecret:@"SECRET"
                             andParentView:self.view
                                 andScopes:[NSArray arrayWithObjects:@"https://www.googleapis.com/auth/userinfo.profile",@"https://www.googleapis.com/auth/userinfo.email", nil]];

And for GTM OAuth 2.0, add this scopehttps://www.googleapis.com/auth/userinfo.email .Hope this helps you.

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49