无法在 iPhone 应用程序中通过 Oauth 2.0 获取 google+ 的访问令牌

2024-02-06

我正在尝试在 iPhone 应用程序中使用 Oauth 2.0 访问 Google+ API。为此,我使用 OauthConsumer 库。我获得了未经授权的 request_token 和授权码,但无法使用授权码将该 request_token 交换为 access_token。我收到错误“invalid_request”。下面是代码片段,我做错了什么或者缺少任何参数吗?

Code:

-(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:)];
} 

仅供参考 - 我不熟悉 Objective-C,但希望 OAuth 的一些知识可以帮助您解决这个问题。

OAuth 1.0 中使用“授权请求令牌” OAuth 2.0 中使用“授权码”

我没有看到任何说 OauthConsumer 支持 OAuth 2.0 的内容

你问: “我做错了什么或者缺少任何参数吗?”

我认为您缺少客户端密钥,这是在 OAuth 2.0 中交换访问令牌的授权代码所必需的。请参阅谷歌 OAuth 2.0 文档 https://developers.google.com/accounts/docs/OAuth2InstalledApp#handlingtheresponse详细了解您需要提供哪些信息来交换授权代码以获取访问令牌。

您可能想查看 Google Toolbox for Mac - OAuth 2 控制器:

http://code.google.com/p/gtm-oauth2/wiki/Introduction http://code.google.com/p/gtm-oauth2/wiki/Introduction

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 iPhone 应用程序中通过 Oauth 2.0 获取 google+ 的访问令牌 的相关文章

随机推荐