java OAuth令牌无效
我正在尝试将Google Plus API集成到我的web应用程序中
我可以通过谷歌认证,并获得代码。当我尝试使用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
。为什么会这样
# 1 楼答案
你确定你以前就这样拿到了代币吗? 在oAuth中,首先调用/authorize获取授权代码,然后使用该代码调用/token获取访问令牌
另外,你可以使用包作为Spring for oAuth,所以它提供了所有“幕后”的东西,你所要做的就是配置XML