1

ios App中的谷歌认证后如何进行?当我允许访问我的应用程序时。出现的窗口是“请复制此代码,切换到您的应用程序并将其复制到那里:”。我不知道如何从这里开始。这是我写的代码

SEL finishedSel = @selector(viewController:finishedWithAuth:error:);

viewController = [[GTMOAuth2ViewControllerTouch 
                                     controllerWithScope:scope
                                      clientID:clientID
                                      clientSecret:clientSecret
                                      keychainItemName:nil
                                      delegate:self                                                     finishedSelector:finishedSel]autorelease];


-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error {
    if (error != nil) {
        // Authentication failed
    } else {
        // Authentication succeeded
    }
}
4

2 回答 2

4

获得身份验证后,
您可以使用它来获取用户数据,请参阅此代码

 NSString *urlStr = @"https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
 NSURL *url = [NSURL URLWithString:urlStr];
 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] autorelease];

                          NSLog(@"output:%@",output);


                      } else {
                          // fetch failed
                          output = [error description];
                      }
                  }

              }];

请注意,您需要在https://www.googleapis.com/auth/userinfo.profile范围内添加此 url

于 2012-05-06T06:45:02.720 回答
0

检查此链接gtm-oauth2

这是finishedWithAuth的示例代码

(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {
      if (error != nil) {
          // Authentication failed (perhaps the user denied access, or closed the
          // window before granting access)
          NSLog(@"Authentication error: %@", error);
          NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey
          if ([responseData length] > 0) {
              // show the body of the server's authentication failure response
              NSString *str = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
              NSLog(@"%@", str);
          }
          self.auth = nil;
      } else {
          // Authentication succeeded
          //
          // At this point, we either use the authentication object to explicitly
          // authorize requests, like
          //
          //  [auth authorizeRequest:myNSURLMutableRequest
          //       completionHandler:^(NSError *error) {
          //         if (error == nil) {
          //           // request here has been authorized
          //         }
          //       }];
          //
          // or store the authentication object into a fetcher or a Google API service
          // object like
          //
          //   [fetcher setAuthorizer:auth];
          // save the authentication object
          self.auth = auth;
      }
}
于 2012-04-30T06:54:19.110 回答