1
我正在尝试将Google Plus API集成到我的Web应用程序中。OAuth令牌invalid_grant
我可以通过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
。这是为什么发生?
是的,我之前获得令牌。更重要的是,我确实得到了代码,但是当试图为令牌交换时出现问题。我不会使用任何图书馆,但如果问题仍然存在,我会看看你建议的那个。 – Dragos
所以第一次调用(/授权)正在工作,但第二次失败。 1.你看到什么错误? 2.尝试调试到HttpClient并查看您尝试发送到服务器的URL。然后,使用REST客户端并尝试使用相同的URL(我们试图找出问题所在......) – OhadR
问题是,我使用httpclient发出请求,但不知何故,我重定向到了我的redirect_uri,这让我做了另一个请求令牌。这是当我得到错误。你能告诉我该怎么做? – Dragos