1

我正在尝试使用 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());
                }
              }
    }

谢谢你帮助我

4

2 回答 2

1

通过引入 redirect_uri 解决了问题

.setRedirectUri(getRedirectUri(req))

并添加方法

  static String getRedirectUri(HttpServletRequest req) {
        GenericUrl requestUrl = new GenericUrl(req.getRequestURL().toString());
        requestUrl.setRawPath("/getauthcodeservlet");
        return requestUrl.build();
      }
于 2014-11-05T19:42:04.720 回答
0

You cannot specify a subdirectory. Per Google's documentation when changing Client Credential settings in the developer's console:

Cannot contain a wildcard (http://*.example.com) or a path (http://example.com/subdir).

So you should change to https://mapmydayunamur.appspot.com

于 2015-04-17T20:12:06.377 回答