【问题标题】:Bad Request 400 when requesting Google OAuth2 token, Java请求 Google OAuth2 令牌时出现错误请求 400,Java
【发布时间】:2018-11-03 05:25:54
【问题描述】:

我通过执行“GET”请求和登录获得了一个身份验证代码,我正在尝试使用该代码通过 http“POST”获取身份验证令牌,但我总是得到响应代码 400。我的代码如下:

// Create HTTP Objects
HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://www.googleapis.com/oauth2/v4/token");

// Add Header
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Create Parameters
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("code", code));
params.add(new BasicNameValuePair("client_id", client_id));
params.add(new BasicNameValuePair("client_secret", client_secret));
params.add(new BasicNameValuePair("redirect_uri", redirect_uri));
params.add(new BasicNameValuePair("grant_type", "authorization_code"));

// Add Parameters and Excecute
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httpPost);

这似乎与他们在此处提供的文档完美匹配: https://developers.google.com/identity/protocols/OAuth2WebServer

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: java http oauth-2.0 google-api


    【解决方案1】:

    好吧,我想通了,这很简单,只要调试我的代码以使来自 Google 的授权代码过期。

    我从 Google 获得了一个新代码并且它有效。

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2013-07-21
      相关资源
      最近更新 更多