Reputation: 22493
How to get the access token & refresh token for google authentication for XMPP. I get successfully the authorization code, but now I need to get the access token & refresh token. But when I do the request in Android with the underneath code I get the response: { "error":"invalid_request" }
HttpPost request = new HttpPost("https://accounts.google.com/o/oauth2/token" );
json.put("client_id", "128232338269.apps.googleusercontent.com" );
json.put("client_secret", "eufZ8Rmjsk1MaADYsHYW" );
json.put("redirect_uri", "urn:ieadsdg:oauth:2.0:oob");
json.put("code", res_code);
json.put("grant_type", "authorization_code");
StringEntity se = new StringEntity(json.toString());
Log.i(TAG, "JSON********" +json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(se);
/* Checking response */
response = client.execute(request);
but i am getting this error for that code. Response = {
"error" : "invalid_request"
}
what is the problem here. HttpPost method is the correct for this url.
Upvotes: 4
Views: 3025
Reputation: 1093
After a chat we found what the problems where. The payload wrong and the content-type was set wrong. The following code is the solution:
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("https://accounts.google.com/o/oauth2/token" );
request.setHeader("Content-type", "application/x-www-form-urlencoded");
//Please make this custom with you're credentials
String requestBody = "code=123123132&client_secret=eufFgyAZ8Rmjsk1MaADYsHYW&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&client_id=128169428269.apps.googleusercontent.com";
try {
request.setEntity(new StringEntity(requestBody));
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
/* Checking response */
try {
HttpResponse response = client.execute(request);
String results = "ERROR";
results = EntityUtils.toString(response.getEntity());
Log.d("STACK", "Response::" + results);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Upvotes: 3