gpt4 book ai didi

java - OAuth token invalid_grant

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:42 27 4
gpt4 key购买 nike

我正在尝试将 Google Plus API 集成到我的网络应用程序中。

我能够通过 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;
}

我之前曾多次设法在模拟应用程序中获取 token ,但现在似乎由于某种未知原因而失败。我得到的只是error:invalid_grant。为什么会发生这种情况?

最佳答案

你确定你之前通过这种方式获得了 token 吗?AFAIK,在 oAuth 中,您首先调用/authorize 来获取授权代码,然后使用此代码调用/token 来获取访问 token 。

附:您可以使用包 Spring for oAuth ,因此它会生成所有“幕后”内容,您所要做的就是配置 XML。

关于java - OAuth token invalid_grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com