在我的iPhone应用中,我使用谷歌登录Oauth2
,我正在关注this insturction并成功登录
- (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];
}
我在GTMOAuth2Authentication
使用https://www.googleapis.com/auth/userinfo.profile范围,现在我想获取用户的基本信息,如姓名,年龄,电子邮件等。
那我怎样才能得到所有细节? 我搜索很多,但没有找到任何东西。
可能重复How to get OAuth2 user information in iOS?的问题,但它也无济于事。
请帮助
答案 0 :(得分:5)
默认情况下,GTMOAuth2ViewControllerTouch viewController将获取用户的电子邮件,但不会获取用户个人资料的其余部分。
登录前可以通过设置此属性从Google服务器请求完整的个人资料:
GTMOAuth2ViewControllerTouch *viewController;
viewController.signIn.shouldFetchGoogleUserProfile = YES;
该登录信息将在登录后显示为
NSDictionary *profile = viewController.signIn.userProfile;
要获取其他信息,您必须更改scope
字符串并再次开始获取。
这里有一些范围网址
@"https://www.googleapis.com/auth/plus.me"
@"https://www.googleapis.com/auth/userinfo.email"
@"https://www.googleapis.com/auth/tasks"
答案 1 :(得分:0)
使用此代码解决了它
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];
}
}
}];