我正在尝试实现Google APIs 2来访问Google OAuth。当我尝试通过访问令牌交换代码时,它不起作用。
我使用此方法来完成此操作。
(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错误
有人能帮我定位这个问题吗?
提前谢谢。
发布于 2012-08-03 11:37:46
由于您使用的是AFNetworking类,因此请尝试使用AFNetworking Extension for OAuth 2 Authentication。我还没用过,但看起来像是你需要的。
https://stackoverflow.com/questions/11761520
复制相似问题