如何在java中使用gdata获取访问令牌
我正在开发一个 Java 应用程序,在其中使用 Java 中的 google gdata 实现 3 足 OAuth。 此应用程序已在 Google App Engine 上注册。在第一阶段,我成功获取未经授权的请求令牌。我将该令牌存储在会话中,并使用 createUserAuthorizationUrl(oauthParameters) 创建链接。然后,单击该链接后,它会将我重定向到“授予访问权限页面”。
现在,即使我授予访问权限,它也不会显示此页面。但是,它会将我重定向到我的回调网址。然而,这似乎是正确的。但是,它也不会在我的帐户下添加条目。在这里,我将 oauth_token 存储在会话中。
重定向时,该页面的 url 包含 oauth_token & oauth_verifier,两者!现在,在这个回调 url 上,我有一个提交按钮和将 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());
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;
}
单击提交按钮时,它会打印“访问令牌:”&没有什么 !没有令牌返回!
我在授权请求令牌本身的阶段出错了。但是,我不明白,产生了什么问题?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您传入
oob
的oauth_callback
时,才会出现包含您链接到的验证器的页面 — 这表明您将把验证器移出带外。我强烈建议不要使用oob
进行除调试之外的任何操作。相反,您应该设置回调 URL 并从查询字符串中获取验证程序。在上面的代码中,我没有看到任何在 OAuth 参数中设置验证程序的内容,因此这可能是您的问题。您在错误处理方面也没有做太多事情,这是 OAuth 流程中非常重要的一部分 - 例如,一旦您让它正常工作,请尝试取消 OAuth 流程并查看您的应用程序如何处理它。
只有在完全完成该过程并获得升级的访问令牌后,您才会在已颁发的令牌列表中看到该条目。
The page with the verifier you linked to should only happen if you pass in an
oauth_callback
ofoob
— this indicates that you will be moving the verifier out-of-band. I strongly recommend against usingoob
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.