日期:2014-05-17  浏览次数:20869 次

豆瓣开放平台, 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: Unauthorized
no auth
at 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参数

------解决方案---------------