Get User information after successful authentication with Oauth2

前端 未结 2 387
暗喜
暗喜 2021-01-25 11:38

In my iPhone app I am using google sign in using Oauth2, I am following this insturction and successfully login in

- (void)viewController:(GTMOAuth         


        
相关标签:
2条回答
  • 2021-01-25 12:31

    By default, the GTMOAuth2ViewControllerTouch viewController will fetch the user's email, but not the rest of the user's profile.
    The full profile can be requested from Google's server by setting this property before sign-in:

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

    The profile will be available after sign-in as

       NSDictionary *profile = viewController.signIn.userProfile;
    

    and to get other information you have to change the scope string and begin fetch again.

    here are some scope urls

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

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

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

    0 讨论(0)
  • 2021-01-25 12:37

    Solved it using this code

     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];
                                  }
                              }
                          }];
    
    0 讨论(0)
提交回复
热议问题