GTMOAuth 2.0似乎是iOS上OAuth 2.0验证的极好工具。我试图通过在Xcode中实现GTMOAuth-2来检索Google用户的全名和电子邮件,但我遇到了一些麻烦。基于这个答案:Retrieve User email using GTM OAuth2 for iOS,它应该像拨打auth.userEmail
一样简单。然而,问题是,在下面的代码段调用auth.userEmail
总是返回null
:iOS上的GTM OAuth 2.0 - 检索用户的电子邮件
- (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);
}
代码成功运行并检索访问令牌,但auth.userEmail
总是null
。我是否需要使用GTMOAuth 2.0的Fetcher
对象向Google电子邮件端点发送请求,或者使用auth.accessToken
发送额外的HTTP GET请求来检索用户的电子邮件?