如何使用java从OpenID + OAuth的请求令牌获取访问令牌

时间:2010-10-22 08:46:12

标签: java openid oauth google-openid

我正在尝试在我的Google应用程序中实现OpenID + OAuth混合协议。 我收到了请求令牌。因此,联合登录中的文档的下一步是访问令牌的交换请求令牌。

我尝试使用OAuth java库,但我没有获得访问令牌。我正在尝试三条腿和两条腿的方法都没有成功。

是否有人成功完成了混合协议。

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

这会抛出com.google.gdata.client.authn.oauth.OAuthException:oauth_token不存在。

oAuthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);和xoauth_requestor_id attibute to feedURL如果我在代码中添加这些行我得到无效的AuthSub令牌异常我不知道为什么它说无效的AuthSub。

1 个答案:

答案 0 :(得分:1)

我的回答here可能对您有帮助。

或者使用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);
    }
}