用于令牌的Oauth 2.0交换授权代码:用于重定向uri的无效方案

时间:2014-11-04 13:40:34

标签: google-app-engine oauth-2.0 google-oauth google-oauth-java-client

我试图在Web应用程序中使用令牌交换收到的授权码,但是当我调用GoogleAuthorizationCodeTokenRequest时,我收到此错误: " redirect_uri的参数值无效: 无效方案:https://mapmydayunamur.appspot.com/getauthcodeservlet"

我尝试了很多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());
                }
              }
    }

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

通过引入redirect_uri

解决了问题
.setRedirectUri(getRedirectUri(req))

并添加方法

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

答案 1 :(得分:0)

您无法指定子目录。在开发人员控制台中更改客户端凭据设置时,根据Google的文档:

  

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

所以你应该改为https://mapmydayunamur.appspot.com