【问题标题】:implement Google OAuth 2实施 Google OAuth 2
【发布时间】:2012-08-01 14:32:22
【问题描述】:

我正在尝试实施 Google OAuth 2 以访问 Google API。 当我尝试通过访问令牌交换代码时,它不起作用。

我就是用这个方法来做的。

 (void)getAccessTokenwWithCode:(NSString*)code
{
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@"https://accounts.google.com/o/oauth2/token" ]];
      [request setHTTPMethod:@"POST"];
      NSDictionary *postBodyDic = [NSDictionary dictionaryWithObjectsAndKeys: 
                             code, @"code", 
                             @"my_client_id", @"client_id",
                             @"my_client_secret", @"client_secret", 
                             @"http://localhost", @"redirect_uri", 
                             @"authorization_code", @"grant_type", 
                             nil];

     NSString *postBody = [postBodyDic JSONString];
     NSData *data = [postBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
     [request setHTTPBody:data];
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];  

     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request                             
     success:^(NSURLRequest *request, NSURLResponse *response, id JSON) 
     {
         NSLog(@"%@", JSON);
     } 
     failure:^(NSURLRequest *request, NSURLResponse *response, NSError *error, id JSON) 
     {
        NSLog(@"ERROR \n%@", error );
     }
     ];

     [queue addOperation:operation];
}

我收到 400 错误

谁能帮我定位问题?

提前致谢。

【问题讨论】:

  • 你能发布完整的错误信息吗?
  • 您将使用哪些 Google API(邮件、日历、分析等)
  • @Claudio Cherubino 这是完整的 eroor 消息 Error Domain=com.alamofire.networking.error Code=-1011 "预期状态代码 [索引数:100(在 1 个范围内) , 索引: (200-299)], got 400" UserInfo=0x1109f850 {NSErrorFailingURLKey=accounts.google.com/o/oauth2/token, NSLocalizedDescription=预期的状态码[索引数: 100 (在1个范围内), 索引: (200- 299)],得到 400}
  • @Mark S 我想使用联系人 api

标签: iphone oauth google-api


【解决方案1】:

由于您使用的是 AFNetworking 类,请尝试使用 AFNetworking Extension for OAuth 2 Authentication。我没用过,不过看起来是你需要的。

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多