如何获取 XMPP 的 google 身份验证的访问令牌和刷新令牌。我成功获得了授权代码,但现在我需要获取访问令牌和刷新令牌。但是当我使用下面的代码在 Android 中执行请求时,我得到了响应:
{
“错误”:“无效请求”
}
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);
但我收到该代码的错误。Response = {
"error" : "invalid_request"
}
这里有什么问题。 HttpPost 方法对于该 url 是正确的。
经过一番交谈我们发现了问题所在。有效负载错误且内容类型设置错误。下面的代码是解决方案:
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.
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)