java - Google API - 重定向 URI 不匹配错误

标签 java oauth-2.0 google-plus

我正在我的网络应用程序上实现 google+ 登录。 我通过引用此link来尝试它.

我的 google+ 登录按钮如下:

<div id="signinButton">
    <span class="g-signin"
    data-scope="https://www.googleapis.com/auth/plus.login"
    data-clientid=[MY_CLIENT_ID] 
    data-accesstype="offline"
    data-cookiepolicy="single_host_origin"
    data-callback="signInCallback"> </span>
</div>

我已将上面链接中给出的js复制到我的网页上。

获得授权代码后,我将其转发到我的 servlet,下面是我用于形成 url 以获取 tokenResponse 的实现。 注意:我没有使用 google 库进行服务器端实现。

这是我的网址形成代码:

String httpsURL = "https://accounts.google.com/o/oauth2/token";

     String query = "code="+code; 
     query += "&";
     query += "client_id=[MY_CLIENT_ID]&";
     query += "client_secret=[CLIENT_SECRET]&";
     query += "redirect_uri=https://localhost:8443/oauth2callback&";
     query += "grant_type=authorization_code";

     try{
     URL myurl = new URL(httpsURL);
     HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
     con.setRequestMethod("POST");
     con.setRequestProperty("Content-length", String.valueOf(query.length()));
     con.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
     con.setDoOutput(true); 
     con.setDoInput(true); 
     con.connect();
     OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
     wr.write(query);
     wr.flush();
     InputStream is;
     if (con.getResponseCode() == 200) {
         is = con.getInputStream();
     } else {
         is = con.getErrorStream();
     }
     //URLConnection con = url.openConnection();
     //conn.setDoOutput(true);
     // Get the response
     BufferedReader rd = new BufferedReader(new InputStreamReader(is));
     String line;
     while ((line = rd.readLine()) != null)
     {
         System.out.println("-----" + line);
     }
     wr.close();
     rd.close();
     }catch(Exception e){
        e.printStackTrace();
     }

在开发人员控制台上,我的重定向 uri 与上面代码示例中给出的相同。

执行完这一切后,每次我都会收到:“error”:“redirect_uri_mismatch”。

对此提出了很多问题,但他们没有解决我的问题。

如有任何建议,我们将不胜感激。提前致谢。

最佳答案

问题在于您使用的是 Google+ 登录,它使用您的重定向 URI。相反,它使用 postmessage 的神奇 URI。

您不需要将 postmessage 添加到开发者控制台(the directions(第 1 步,第 7c 项)通常会说完全清除此字段),但您需要这样做 当您用代码交换 token 时需要使用它(请参阅 step 8 中的示例代码)。

所以你上面的代码应该读起来更像

 String query = "code="+code; 
 query += "&";
 query += "client_id=[MY_CLIENT_ID]&";
 query += "client_secret=[CLIENT_SECRET]&";
 query += "redirect_uri=postmessage&";
 query += "grant_type=authorization_code";

关于java - Google API - 重定向 URI 不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25181890/

相关文章:

.net - OpenId Connect - 可以使用客户端证书进行身份验证吗?

android - Android 应用程序中的 Google Plus 身份验证

java - Spring Boot OAuth2 具有基本身份验证和自定义 UserDetailsS​​ervice

javascript - Google plus share 设置自定义标题、描述、图像并且不使用 og meta

ruby - Oauth2 谷歌 API ruby 客户端 : How to set Approval Prompt to Auto?

java - 无法加载 Java 库

java - 在运行时确定应用程序服务器?

javascript - 如何为google+和twitter的 "like button"分配动态链接

java - 在微服务中使用 session 是错误的吗?

java - 在 Eclipse 下的 Android 项目中包含另一个(非 Android)项目的最佳方法是什么