使用Oauth2成功验证后获取用户信息
问题描述:
在我的iPhone应用中,我使用 Oauth2
进行谷歌登录,我正在关注此项目并成功登入
In my iPhone app I am using google sign in using Oauth2
, I am following this insturction and successfully login in
- (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController
finishedWithAuth:(GTMOAuth2Authentication * )auth
error:(NSError * )error
{
if(!error)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Success Authorizing with Google"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
我正在使用 https://www.googleapis.com/auth/userinfo.profile 范围 GTMOAuth2Authentication
,现在我想获得用户的基本信息,如姓名,年龄,电子邮件等。
I am using https://www.googleapis.com/auth/userinfo.profile scope for GTMOAuth2Authentication
, now I want to get user basic information like name,age,email,etc.
那么我怎样才能获得所有细节?
我搜索了很多,但没有找到任何东西。
So how can I get the all detail??
I have searching lot but didn't find any thing.
可能重复的问题如何在iOS中获取OAuth2用户信息?,但它也无济于事。
Probably duplicate question of How to get OAuth2 user information in iOS? , but it also doesn't help.
请帮助
答
使用此代码解决它
NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/plus/v1/people/me/activities/public"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[self.auth authorizeRequest:request
completionHandler:^(NSError *error) {
NSString *output = nil;
if (error) {
output = [error description];
} else {
// Synchronous fetches like this are a really bad idea in Cocoa applications
//
// For a very easy async alternative, we could use GTMHTTPFetcher
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if (data) {
// API fetch succeeded
output = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] ;
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSArray *array=[json objectForKey:@"items"];
if (array.count>0) {
NSDictionary *dicts=[array objectAtIndex:0];
// NSLog(@"actor:%@",[dicts objectForKey:@"actor"]);
//[self updateUI:[dicts objectForKey:@"actor"]];
// [dictUserInfo setObject:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"] forKey:@"imageUrl"];
if([delegate respondsToSelector:@selector(userPicChanged:)])
{
//send the delegate function with the amount entered by the user
[delegate userPicChanged:[[[dicts objectForKey:@"actor"] objectForKey:@"image"]objectForKey:@"url"]];
}
}
} else {
// fetch failed
output = [error description];
}
}
}];