我的ios应用程序在GTMOAth2上登录gmail。而且,我可以获得gplus朋友的电子邮件,还是可以使用GTMOAth2令牌获取gplus信息?
答案 0 :(得分:1)
1.创建按钮
signIn = [GPPSignIn sharedInstance];
signIn.delegate = self;
signIn.clientID = kClientId;
signIn.shouldFetchGooglePlusUser = YES;
signIn.shouldFetchGoogleUserEmail = YES;
signIn.homeServerClientID = kClientId;
// Uncomment one of these two statements for the scope you chose in the previous step
// signIn.scopes = @[ kGTLAuthScopePlusLogin ]; // "https://www.googleapis.com/auth/plus.login" scope
signIn.scopes = @[ @"profile" ];
signIn.shouldFetchGoogleUserEmail=YES;
[signIn trySilentAuthentication];
2.登录验证
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error {
NSLog(@"Received error %@ and auth object %@",error, auth);
if(error)
{
}
else
{
NSString *serverCode = [GPPSignIn sharedInstance].homeServerAuthorizationCode;
NSLog(@"this is server code%@",serverCode);
NSLog(@"user email%@", signIn.authentication.userEmail);
NSLog(@"user id%@",signIn.authentication.userID);
NSLog(@"auth token=%@",auth.accessToken);
NSString *str = [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",auth.accessToken];
NSString* escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
NSData *data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
name = json[@"given_name" ];
email=json[@"email"];
imgurl=json[@"picture"];
NSLog(@"this is email=%@name==%@picture url%@",email,name,imgurl);
}
如果您想使用令牌,请使用上面的代码......希望这会对您有所帮助。