当前位置: 代码迷 >> Java Web开发 >> 豆瓣开放平台, oauth 认证获取getAccessToken() 失败解决方案
  详细解决方案

豆瓣开放平台, oauth 认证获取getAccessToken() 失败解决方案

热度:9289   发布时间:2013-02-25 21:17:36.0
豆瓣开放平台, oauth 认证获取getAccessToken() 失败
这是授权时的url 
http://www.douban.com/service/auth/authorize?

  oauth_token=957cafde5a115a79c0760f9b0e340f6b&oauth_callback=http://localhost:8080/ART/user/doubanOAuth.action

这是授权后的跳转的url

  http://localhost:8080/ART/user/doubanOAuth.php?oauth_token=957cafde5a115a79c0760f9b0e340f6b

通过用户授权后,通过response重定向到 doubanLogin.action

  response.sendRedirect(myService.getAuthorizationUrl("http://localhost:8080/ART/user/doubanLogin.action"));

下面是doubanLogin.action 里面的代码

Java code
 String requestToken = request.getParameter("oauth_token");        DoubanService myService = new DoubanService("subApplication", apiKey, secret);        if(requestToken != null) {            myService=(DoubanService) request.getSession().getAttribute("DoubanService");//            // step2 : user give the authorization and prepare to get the request token                Cookie[] cookies = request.getCookies();                if (cookies == null) {                    System.err.println("request token secret not found in cookie");                    return;                }                Cookie c = cookies[0];//                // set request token and token secret                myService.setRequestTokenSecret(c.getValue());                myService.setRequestToken(requestToken);//                // get request token                [color=#FF0000]myService.getAccessToken();[/color]

  在红色的这一步出现异常。(经过反复的测试,只要我用重定向的方式,就会出错,但是把授权时的url写在页面上的A标签里面,就能正确运行。) 下面是异常:
Java code
net.oauth.OAuthProblemException    at net.oauth.client.OAuthHttpClient.invoke(OAuthHttpClient.java:71)    at net.oauth.client.OAuthClient.invoke(OAuthClient.java:96)    at net.oauth.client.OAuthClient.invoke(OAuthClient.java:102)    at com.google.gdata.client.douban.DoubanService.getAccessToken(Unknown Source)    at com.art.action.UserAction.doubanLogin(UserAction.java:500)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)com.google.gdata.util.AuthenticationException: Unauthorizedno authat com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:570)    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:559)    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:542)    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:521)    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:531)    at com.google.gdata.client.Service.getEntry(Service.java:982)    at com.google.gdata.client.Service.getEntry(Service.java:908)    at com.google.gdata.client.douban.DoubanService.getEntry(Unknown Source)    at com.google.gdata.client.douban.DoubanService.getAuthorizedUser(Unknown Source)    at com.art.action.UserAction.doubanLogin(UserAction.java:505)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


我实在不能理解这2者有什么区别? 有哪位大神搞过豆瓣的oauth 认证的求指导,万分感谢!

这是豆瓣API中提到的,没能完全理解: 然而进行POST、PUT、DELETE请求时,豆瓣暂时不支持使用在url中或者post form中传递OAuth参数。因此你只能选择在header中传递OAuth参数

------解决方案--------------------------------------------------------
com.google.gdata.util.AuthenticationException: Unauthorized
no auth

看样子应该是没有授权把,你的access_token是怎样取的?我只弄过oauth2。2的话,获取access_token需要传应用id和密钥和code参数还有回调地址。oauth1就不清楚了。是不是漏传了什么参数啊?
  相关解决方案