iphone - 在iOS App中执行Google身份验证后如何进行

标签 iphone objective-c ios google-authentication

在iOS App中执行Google身份验证后如何进行?当我允许我的应用程序访问时。出现的窗口是“请复制此代码,切换到您的应用程序并将其复制到此处:”。我不知道如何从此处继续。
这是我写的代码

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

最佳答案

获得认证后
您可以使用它来获取用户数据,请参见以下代码

 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中添加此网址

关于iphone - 在iOS App中执行Google身份验证后如何进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10378641/

相关文章:

iphone - ASIHttprequest 用于登录认证

iphone - 对创建一个同时具有 iphone 和 iPad 版本的应用程序感到困惑

iPhone 4 是否有绝对确定的方式让 NSTimer 长期运行

objective-c - 什么是 NSCache?

ios - iOS 11.1.2 上的 Xamarin.Forms : Webview loads a bigger font

ios - 弱委托(delegate)和类协议(protocol)

iphone - 使用 Web 服务的 AFNetworking 身份验证

iphone - 创建一个类以从 Web 服务上的异步调用返回数据

objective-c - 以线程安全的方式启动和停止操作

ios - 从设备中删除本地 icloud 文件?