【发布时间】: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