我正在尝试在 iPhone 应用程序中使用 Oauth 2.0 访问 Google+ API。为此,我正在使用 OauthConsumer 库。我得到了未经授权的 request_token 和授权码,但无法使用授权码将该 request_token 交换为 access_token。我收到错误为“invalid_request”。下面是代码片段,我是否做错了什么或缺少任何参数?
代码:
-(void)getAccessTokenWithAuthorizationCode:(NSString *)code
{
NSURL *accessTokenURL = [NSURL URLWithString:@"https://accounts.google.com/o/oauth2/token"];
OAMutableURLRequest *accessRequest = [[OAMutableURLRequest alloc] initWithURL:accessTokenURL
consumer:consumer
token:requestToken
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
[accessRequest setHTTPMethod:@"POST"];
OARequestParameter *authCode = [[OARequestParameter alloc] initWithName:@"code" value:code];
OARequestParameter *redirectURI = [[OARequestParameter alloc] initWithName:@"redirect_uri" value:kRedirectURI];
OARequestParameter *granType = [[OARequestParameter alloc] initWithName:@"grant_type" value:@"authorization_code"];
[accessRequest setParameters:[NSArray arrayWithObjects:authCode, redirectURI, granType, nil]];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:accessRequest
delegate:self
didFinishSelector:@selector(accessTokenTicket:didFinishWithData:)
didFailSelector:@selector(accessTokenTicket:didFailWithError:)];
}