1

在我的 iPhone 应用程序中,我正在使用 google 登录Oauth2,我正在遵循这个指令并成功登录

- (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 用户信息?,但这也无济于事。

请帮忙

4

2 回答 2

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"

于 2013-08-01T10:19:54.187 回答
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];
                              }
                          }
                      }];
于 2013-08-05T11:33:02.830 回答