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