【问题标题】:Get User information after successful authentication with Oauth2使用 Oauth2 认证成功后获取用户信息
【发布时间】:2013-08-02 03:04:18
【问题描述】:

在我的 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? 的问题可能重复,但也无济于事。

请帮忙

【问题讨论】:

  • 您必须将您的 viewController 的 signIn.shouldFetchGoogleUserProfile 设置为 YES。 viewController.signIn.shouldFetchGoogleUserProfile = YES;
  • 请检查我的答案,如果它不起作用,请告诉我。 @tosa

标签: iphone ios oauth-2.0 google-oauth


【解决方案1】:

默认情况下,GTMOAuth2ViewControllerTouch 视图控制器将获取用户的电子邮件,但不获取用户个人资料的其余部分。
通过在登录前设置此属性,可以从 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"

【讨论】:

  • 您能展示一下您尝试过的内容吗?你在哪里卡住了?
  • 我必须说,我遇到了其他 SDK 问题,显示“在 Google 上知道你是谁”。我的用户不想看到的。所以这个同样的修复就像魅力一样。非常感谢 +1
【解决方案2】:

用这段代码解决了

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

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 2016-06-10
    • 2019-05-03
    • 1970-01-01
    • 2021-03-27
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多