Not able to get access token for google+ through O

2019-07-15 12:34发布

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条回答
Root(大扎)
2楼-- · 2019-07-15 13:20

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

查看更多
登录 后发表回答