Get User information after successful authentication with Oauth2

前端 未结 2 386
暗喜
暗喜 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: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];
                                  }
                              }
                          }];
    

提交回复
热议问题