Not able to get access token for google+ through Oauth 2.0 in iPhone application

不问归期 提交于 2019-12-14 03:56:58

问题


I am trying to access Google+ APIs with Oauth 2.0 in iPhone Application. For this purpose I am using OauthConsumer library. I got the unauthorized request_token and also authorization code but not able to exchange that request_token for access_token using authorization code. I am getting error as "invalid_request". Below is the code snippet, am I doing anything wrong or missing any parameter?

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

回答1:


FYI - I'm not familiar with Objective-C, but hopefully some knowledge of OAuth will help you figure this out.

"authorized request tokens" are used in OAuth 1.0 "authorization codes" are used in OAuth 2.0

I'm not seeing anything saying that OauthConsumer supports OAuth 2.0

You asked: "am I doing anything wrong or missing any parameter?"

I think you are missing the client secret, which is necessary to exchange an authorization code for an access token in OAuth 2.0. See the Google OAuth 2.0 documentation for more information on what you need to provide to exchange an authorization code for an access token.

You might want to check out the Google Toolbox for Mac - OAuth 2 Controllers:

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



来源:https://stackoverflow.com/questions/10933829/not-able-to-get-access-token-for-google-through-oauth-2-0-in-iphone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!