如何在Java中使用gdata获取访问令牌 [英] how to get access token using gdata in java

查看:118
本文介绍了如何在Java中使用gdata获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Java应用程序,在其中使用Google gdata在Java中实现三足式OAuth。 此应用已在Google App Engine中注册。在第一阶段,我成功获取了未经授权的请求令牌。我将令牌存储在会话中,并使用createUserAuthorizationUrl(oauthParameters)创建一个链接。然后,单击链接,它将我重定向到授权访问页面。



现在,即使我授予访问权限,它也不会显示此页面。但是,它将我重定向到我的回调URL。但是,这似乎是正确的。但是,它也不会在我的帐户下添加条目。



重定向后,该页面的网址包含oauth_token& oauth_verifier,两者!现在,在此回调网址上,我有一个提交按钮&将for的操作设置为accessTokenServlet.java。该servlet的代码如下:



现在,我正在发送请求以获取访问令牌。我的代码是:

  GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH);

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());

oauthParameters.setOAuthToken(request.getSession()。getAttribute( oauth_token)。toString());
oauthParameters.setOAuthTokenSecret(request.getSession()。getAttribute( oauth_token_secret)。toString());

试试{
字符串accessToken = oauthHelper.getAccessToken(oauthParameters);
out.println(访问令牌: + accessToken);
} catch(OAuthException e){
//System.out.print(\"Response Status: + response.getStatus());
out.println( Exception:);
e.printStackTrace();
的回报;
}

在单击提交按钮时,它会打印访问令牌:和没有 !没有令牌返回!



在授权请求令牌本身的阶段,我错了。但是,我没有得到,产生了什么问题?

解决方案

包含验证程序的页面仅当您传递 oob oauth_callback 时,才发生链接到—这表明您将把验证程序移出频段。我强烈建议不要在调试中使用 oob 。相反,您应该设置一个回调URL并从查询字符串中获取验证器。



在上面的代码中,我看不到任何将验证器设置为OAuth参数,因此可能是您遇到的问题。您在错误处理方面也没有做很多事情,这是OAuth流程中非常重要的部分-例如,一旦工作正常,请尝试取消OAuth流程,然后查看应用程序如何处理它。 / p>

完全完成该过程并获得升级的访问令牌后,您只会在已发行令牌列表中看到该条目。


I am developing a Java Application where I am implementing 3-legged OAuth using google gdata in Java. This application is registered on Google App Engine. At the first stage, I am getting the unauthorized request-token successfully. I am storing that token in session and create a link using createUserAuthorizationUrl(oauthParameters). Then on clicking the link, it redirect me to "Grant Access Page".

Now, even though I grant access, it doesn't show me this page. But, it redirects me to my callback url. However, this seems proper. But, it also doesn't add the entry under My Account. Here, I am storing the oauth_token in session.

When getting redirected, the url of that page contains oauth_token & oauth_verifier, both ! Now, on this callback url, I have a submit button & set action of for to an accessTokenServlet.java. The code of this servlet is as follow :

Now I am sending request to fetch Access Token. My code is :

            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
            oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
            oauthParameters.setOAuthType(OAuthParameters.OAuthType.THREE_LEGGED_OAUTH);

            GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());

            oauthParameters.setOAuthToken(request.getSession().getAttribute("oauth_token").toString());
            oauthParameters.setOAuthTokenSecret(request.getSession().getAttribute("oauth_token_secret").toString());

            try {
                String accessToken = oauthHelper.getAccessToken(oauthParameters);
                out.println("Access Token : " + accessToken);
            } catch (OAuthException e) {
                //System.out.print("Response Status : " + response.getStatus());
                out.println("Exception : ");
                e.printStackTrace();
                return;
            }

While clicking on submit button, it prints "Access Token : " & nothing ! No token returns !

I am getting wrong at the stage of authorizing the request token itself. But, I am not getting, what problem got generated ?

解决方案

The page with the verifier you linked to should only happen if you pass in an oauth_callback of oob — this indicates that you will be moving the verifier out-of-band. I strongly recommend against using oob for anything but debugging. Instead, you should be setting a callback URL and getting the verifier out of the query string.

In the code above, I don't see anything that sets the verifier in the OAuth parameters, so that's likely your problem. You're also not doing much in the way of error handling, and that's a really important piece of the OAuth flow — for example, once you've got it working, try canceling the OAuth process and see how your application handles it.

You will only see the entry in your issued tokens list after you've fully completed the process and obtained an upgraded access token.

这篇关于如何在Java中使用gdata获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆