问题
I am implementing google+ signin on my web application. I tried it by referencing from this link.
My google+ sign in button is as follows:
<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>
I have copied js given on above link on my webpage.
After getting authorization code, I am forwarding this to my servlet, below is my implementation for forming url to get tokenResponse. Note: I am not using google libraries for server side implementations.
Here is my url forming code:
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();
}
On developer console, my redirect-uri is same as given in above code sample.
After executing all this, every time I get this : "error" : "redirect_uri_mismatch".
There are so many questions asked on this, but they didn't solved my problem.
Any suggestion would be appreciated. Thanks in advance.
回答1:
The issue is that you are using the Google+ Sign-In, which does not use your Redirect URI. Instead, it uses a magic URI of postmessage
.
You don't need to add postmessage
to the developer console (the directions (step 1, item 7c) usually say to clear this field completely), but you do need to use it when you're exchanging the code for tokens (see the sample code in step 8).
So your code above should read something more like
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";
来源:https://stackoverflow.com/questions/25181890/google-api-redirect-uri-mismatch-error