开发者

how to get access token from request token for OpenID + OAuth using java

I am trying to implement the OpenID + OAuth hybrid protocol in my application for Google. I am getting the request token. So the next step as document in the Federated Login is exchange request token for an access token.

I tried it using OAuth java library but i am not getting the access token. I am trying both the 3-legged and 2-legged approaches not succeed.

Is anyone succeed in doing the hybrid protocol.

    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
        oauthParameters.setOAuthConsumerKey(consumerKey);
        oauthParameters.setOAuthConsumerSecret(consumerSecret);

        calendarService = new CalendarService("marketplace-hello");
        try {
            calendarService.开发者_如何学编程setOAuthCredentials(oauthParameters, 
new OAuthHmacSha1Signer());
            CalendarEventFeed results = calendarService.query(calendarFeedUrl, 
CalendarFeed.class);
        }
 catch (OAuthException e) 
{      
throw new ServletException("Unable to initialize calendar service", e); 
}

This is throwing the com.google.gdata.client.authn.oauth.OAuthException: oauth_token does not exist.

oAuthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH); and xoauth_requestor_id attibute to feedURL if i add these lines in the code i am getting Invalid AuthSub Token exception i don't know why it is saying Invalid AuthSub.


My answer here may help you.

Or try this with your requestToken:

import net.oauth.OAuth;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.OAuthServiceProvider;
import net.oauth.client.OAuthClient;
import net.oauth.client.httpclient4.HttpClient4;

public class Try {

    public static void doit(String requestToken) throws Exception {

        String requestUrl = "https://www.google.com/accounts/OAuthGetRequestToken";
        String authorizeUrl = "https://www.google.com/accounts/OAuthAuthorizeToken";
        String accessUrl = "https://www.google.com/accounts/OAuthGetAccessToken";
        String consumerKey = "XXXXX";
        String consumerSecret = "XXXXX";
        String callbackUrl = "XXXXX";

        OAuthServiceProvider provider = new OAuthServiceProvider(requestUrl,
                authorizeUrl, accessUrl);

        OAuthConsumer consumer = new OAuthConsumer(callbackUrl, consumerKey,
                consumerSecret, provider);

        consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);

        OAuthClient client = new OAuthClient(new HttpClient4());

        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.requestToken = requestToken;

        OAuthMessage result = client.getAccessToken(accessor, null, null);

        System.out.println(accessor.accessToken);
        System.out.println(accessor.tokenSecret);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜