[英]OAuth token invalid_grant
I am trying to integrate the Google Plus API into my web application. 我正在尝试将Google Plus API集成到我的Web应用程序中。
I am able to authenticate with Google and I get the code. 我可以通过Google进行身份验证,并且可以获得代码。 The problem occurs when I try getting an
access_token
using HttpClient
. 当我尝试使用
HttpClient
获取access_token
时出现问题。
Here is some code: 这是一些代码:
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;
}
I have managed to get the token in my mock application several times before, but now it seems to be failing for some unknown reason. 以前,我已经设法在我的模拟应用程序中获得了令牌,但是现在由于某种未知的原因,它似乎失败了。 All I get is
error:invalid_grant
. 我得到的只是
error:invalid_grant
。 Why is this happening? 为什么会这样呢?
are you sure u got the token this way before? 您确定以前以这种方式获得令牌吗? AFAIK, in oAuth, you first call /authorize to get the authorization code, and then, using this code, you make the call to /token to get the access token.
AFAIK,在oAuth中,您首先调用/ authorize以获取授权代码,然后使用该代码对/ token进行调用以获取访问令牌。
ps you can use packages as Spring for oAuth , so it makes all the "behind-the-scenes" stuff, all you have to do is to configure the XML. ps,您可以将包用作Spring的oAuth ,这样它就可以完成所有“幕后”工作,所有您需要做的就是配置XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.