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?