After a lot of time wasted googling for the possible reasons for a 'Bad Request' when requesting for a token at https://accounts.google.com/o/oauth2/token, I decided to ask why this code can't obtain nothing but a 'bad request' response from the server...
String url = "https://accounts.google.com/o/oauth2/token";
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setChunkedStreamingMode(0);
con.setRequestMethod("POST");
con.setRequestProperty("Host", "accounts.google.com");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setRequestProperty("code", authCode);
con.setRequestProperty("client_id",
"[CLIENT_ID]");
con.setRequestProperty("client_secret", "[CLIENT_SECRET");
con.setRequestProperty("redirect_uri",
"http://localhost:8080/login");
con.setRequestProperty("grant_type", "authorization_code");
// Send post request
con.setDoOutput(true);
I did have to set con.setChunkedStreamingMode(0)
because the server was returning an error related to content length.
Any ideas? Could it be necessary to put the payload in a single line? how?