我正在我的网络应用程序上实现google + signin。 我通过参考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。 注意:我没有使用谷歌库进行服务器端实现。
这是我的网址格式代码:
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();
}
在开发者控制台上,我的redirect-uri与上面代码示例中给出的相同。
执行完所有这些后,每次我得到这个:&#34;错误&#34; :&#34; redirect_uri_mismatch&#34;。
关于这个问题有很多问题,但它们并没有解决我的问题。
任何建议都将不胜感激。提前谢谢。
答案 0 :(得分:4)
问题是您使用的是Google+登录,不使用您的重定向URI。相反,它使用了postmessage
的神奇URI。
您无需将postmessage
添加到开发者控制台(the directions(步骤1,项目7c)通常说要完全清除此字段),但您执行在交换令牌代码时需要使用它(参见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";