我正在尝试实施 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 错误
任何人都可以帮我定位问题吗?
提前致谢。