1

我試圖在Web應用程序中交換一個令牌接收到的授權碼,但當我調用GoogleAuthorizationCodeTokenRequest時,出現此錯誤: 「無效的redirect_uri參數值: 無效的計劃:https://mapmydayunamur.appspot.com/getauthcodeservletOauth 2.0交換授權碼令牌:無效的重定向方案uri

我試過很多redirect_uri,不知道爲什麼我得到這個錯誤。 Uri在我的開發者控制檯中重定向Uri。 這裏是我的代碼: 在getauthcodeservlet.java:

String authcode = req.getParameter("code"); 
     String clientID = "CLIENTID_FROM_GOOGLEDEVELOPERSCONSOLE" 
     String clientSecret = "CLIENTSECRET_FROM_GOOGLEDEVELOPERSCONSOLE"; 
     String redirectUri = "https://mapmydayunamur.appspot.com/getauthcodeservlet"; 

try { 
       GoogleTokenResponse response = 
        new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), new JacksonFactory(),clientID, clientSecret, authcode,redirectUri).execute(); 
       resp.getWriter().println("Access token: " + response.getAccessToken()); 
       } catch (TokenResponseException e) { 
       if (e.getDetails() != null) { 
        resp.getWriter().println("Error: " + e.getDetails().getError()); 
        if (e.getDetails().getErrorDescription() != null) { 
         resp.getWriter().println(e.getDetails().getErrorDescription()); 
        } 
        if (e.getDetails().getErrorUri() != null) { 
         resp.getWriter().println(e.getDetails().getErrorUri()); 
        } 
       } else { 
        resp.getWriter().println(e.getMessage()); 
       } 
       } 
    } 

謝謝你幫我

+0

空格字符在HTTPS的面前? 「HTTPS ... – pinoyyid 2014-11-04 16:56:11

+0

對不起時複製粘貼我的代碼進入論壇。 現在,它的良好的版本,不適合你的答案既不工作.. 還是要謝謝你! – 2014-11-04 17:54:15

回答

1

問題由inruducing的REDIRECT_URI解決通過

.setRedirectUri(getRedirectUri(req)) 

並添加方法

static String getRedirectUri(HttpServletRequest req) { 
     GenericUrl requestUrl = new GenericUrl(req.getRequestURL().toString()); 
     requestUrl.setRawPath("/getauthcodeservlet"); 
     return requestUrl.build(); 
     } 
+0

介意分享整個代碼,這是一個錯誤? (Servlet的) – 2016-12-18 12:21:00

0

你不能指定一個子目錄。每谷歌文檔中的開發者控制檯改變客戶機憑證設置時:

不能包含通配符(HTTP://*.example.com)或路徑 (http://example.com/subdir)。

所以,你應該改變https://mapmydayunamur.appspot.com