首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实现谷歌OAuth 2

实现谷歌OAuth 2
EN

Stack Overflow用户
提问于 2012-08-01 22:32:22
回答 1查看 625关注 0票数 0

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

我使用此方法来完成此操作。

代码语言:javascript
复制
 (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错误

有人能帮我定位这个问题吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-08-03 11:37:46

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11761520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档