[英]OAuth token invalid_grant
我正在尝试将Google Plus API集成到我的Web应用程序中。
我可以通过Google进行身份验证,并且可以获得代码。 当我尝试使用HttpClient
获取access_token
时出现问题。
这是一些代码:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://accounts.google.com/o/oauth2/token");
post.addHeader("accept", "application/json");
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("code", myCode));
nvps.add(new BasicNameValuePair("client_id",
"my_client_id"));
nvps.add(new BasicNameValuePair("redirect_uri",
"http://localhost:8080/test/gplus.do"));
nvps.add(new BasicNameValuePair("client_secret",
"my_client_secret"));
nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
res = res + output;
}
以前,我已经设法在我的模拟应用程序中获得了令牌,但是现在由于某种未知的原因,它似乎失败了。 我得到的只是error:invalid_grant
。 为什么会这样呢?
您确定以前以这种方式获得令牌吗? AFAIK,在oAuth中,您首先调用/ authorize以获取授权代码,然后使用该代码对/ token进行调用以获取访问令牌。
ps,您可以将包用作Spring的oAuth ,这样它就可以完成所有“幕后”工作,所有您需要做的就是配置XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.