我已经在我的 ios 应用程序中集成了 google plus,我能够成功登录,但我无法获取当前登录用户的电子邮件 ID。我参考了https://developers.google.com/+/ mobile/ios/并已完成登录所需的所有步骤!
那么,我如何获得在 Google plus 中登录的当前用户邮件 ID?
我已经在我的 ios 应用程序中集成了 google plus,我能够成功登录,但我无法获取当前登录用户的电子邮件 ID。我参考了https://developers.google.com/+/ mobile/ios/并已完成登录所需的所有步骤!
那么,我如何获得在 Google plus 中登录的当前用户邮件 ID?
转到 dic 中的 GTMOAuth2Authentication.m 文件方法 setKeysForResponseDictionary 在方法结束时返回访问令牌。
accessTocken = [dict valueForKey:@"access_token"]; // access tocken pass in .pch file
[accessTocken retain];
在你的控制器中
- (IBAction)momentButton:(id)sender {
NSString *str = [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];
NSString* escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
NSMutableDictionary *proDic = [[NSMutableDictionary alloc] init];
proDic=[jsonData JSONValue];
NSLog(@"%@",proDic);
这是获取当前登录用户的电子邮件 ID 的最简单和最简单的方法
首先创建 GPPSignIn 类的实例变量
GPPSignIn *signIn;
然后在viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
static NSString * const kClientID = @"your client id";
signIn = [GPPSignIn sharedInstance];
signIn.clientID= kClientID;
signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil];
signIn.shouldFetchGoogleUserID=YES;
signIn.shouldFetchGoogleUserEmail=YES;
signIn.delegate=self;
}
接下来GPPSignInDelegate
在您的视图控制器中实现,您
可以获得登录用户的电子邮件 ID
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error
{
NSLog(@"Received Access Token:%@",auth);
NSLog(@"user google user id %@",signIn.userEmail); //logged in user's email id
}