oauth问题(app引擎)

时间:2009-11-08 19:18:30

标签: google-app-engine oauth

我正在尝试使用oauth从谷歌文档中提取用户的文档数据, 但我无法理解如何做到这一点 - oauth_verifier的目的是什么? - 如何获取访问令牌的秘密? - 如果我尝试使用下面的DocsService,那么我有一个“服务器错误”
- 有没有明确的教程?我找不到任何动静..

    String oauth_verifier = req.getParameter("oauth_verifier");
    String oauth_token = req.getParameter("oauth_token");
    String oauthtokensecret = req.getParameter("oauth_token_secret");

    GoogleOAuthParameters oauthparam = new GoogleOAuthParameters();
    oauthparam.setOAuthConsumerKey("consumer key");
    oauthparam.setOAuthConsumerSecret("secret");
    oauthparam.setOAuthToken(oauth_token);
    oauthparam.setOAuthTokenSecret(oauthtokensecret);
    oauthparam.setOAuthVerifier(oauth_verifier);

    OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();
    GoogleOAuthHelper oauthhelper = new GoogleOAuthHelper(signer);
    String accesstoken = "";
    String accesstokensecret = "";

    try {
        oauthhelper.getUnauthorizedRequestToken(oauthparam);
        accesstoken = oauthhelper.getAccessToken(oauthparam);
        accesstokensecret = oauthparam.getOAuthTokenSecret();

// DocsService client = new DocsService(“yourCompany-YourAppName-v1”);     ...

2 个答案:

答案 0 :(得分:0)

这些可能不是您想要的,因为它们是OAuth特定的而不是与谷歌相关的,但我发现这些“入门”文章非常有用:

http://oauth.net/documentation/getting-started

答案 1 :(得分:0)

原来我需要获取oauth_token_secret并在以后重用它。 所以(在将用户重定向到谷歌登录页面之前)

oauthhelper.getUnauthorizedRequestToken(oauthparam);
requesturl = oauthhelper.createUserAuthorizationUrl(oauthparam);
OAuthTokenSecret.tokenSecret = oauthparam.getOAuthTokenSecret();
resp.sendRedirect(requesturl);

然后在用户授予访问权限后,我们已被重定向到oauth_callback网址:

oauthparam.setOAuthToken(oauthtoken);
oauthparam.setOAuthVerifier(oauthverifier);
oauthparam.setOAuthTokenSecret(OAuthTokenSecret.tokenSecret);
oauthhelper.getAccessToken(oauthparam); // access token and access token secret are saved in oauthparam.
// access google service..
GoogleService googleService = new GoogleService( "cp", "test222");
googleService.setOAuthCredentials(oauthparam, signer);
BaseFeed resultFeed = googleService.getFeed(feedUrl, Feed.class);