使用Oauth2成功validation后获取用户信息

在我的iPhone应用程序中,我正在使用谷歌login使用Oauth2 ,我正在跟随这个insturction,并成功login

 - (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 ,现在我想获得用户的基本信息,如姓名,年龄,电子邮件等。

那么我怎么能得到所有的细节?
我搜寻很多,但没有发现任何东西。

可能重复的问题如何获得iOS中的OAuth2用户信息? ,但它也没有帮助。

请帮忙

默认情况下,GTMOAuth2ViewControllerTouch viewController将获取用户的电子邮件,而不是用户的configuration文件的其余部分。
完整的configuration文件可以通过在login前设置此属性从Google的服务器请求:

GTMOAuth2ViewControllerTouch *viewController; viewController.signIn.shouldFetchGoogleUserProfile = YES;

该configuration文件将在login后可用

  NSDictionary *profile = viewController.signIn.userProfile; 

并得到其他信息,你必须改变scopestring,并再次开始提取。

这里有一些范围的URL

@"https://www.googleapis.com/auth/plus.me"

@"https://www.googleapis.com/auth/userinfo.email"

@"https://www.googleapis.com/auth/tasks"

用这个代码解决它

  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]; } } }];