简体   繁体   English

为什么Google OAuth 2.0代码令牌交换会返回“ invalid_request”?

[英]Why does Google OAuth 2.0 code-token Exchange returns “invalid_request”?

I have successfully got the authorization_code . 我已经成功获得了authorization_code And getting "Error" : "invalid_request" in response for authorization_code-token exchange . 并获得"Error" : "invalid_request"作为对authorization_code-token exchange响应。
Here is my java code to get the Google OAuth token in exchange to authentication_code: 这是我的Java代码,用于获取Google OAuth令牌以交换authentication_code:
(Made use of HttpComponents for HTTP requests) (对HTTP请求使用HttpComponents

String urlString = "https://accounts.google.com/o/oauth2/token";
String client_id = "<my_client_id>";
String client_secret = "<my_client_secret>";
String redirect_uri = "<my_redirect_url>";
String grant_type = "authorization_code";
HttpParams params = new BasicHttpParams();
params.setParameter("code", code);
params.setParameter("client_id", client_id);
params.setParameter("client_secret", client_secret);
params.setParameter("redirect_uri", redirect_uri);
params.setParameter("grant_type", grant_type);
HttpPost post = new HttpPost(urlString);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.setParams(params);
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
    HttpResponse response = httpClient.execute(post);
    HttpEntity entity = response.getEntity();
    System.out.println(response.toString());
    DataInputStream in = new DataInputStream(entity.getContent());
    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
}

Getting the following Error in response: 得到以下错误作为响应:

HTTP/1.1 400 Bad Request [Cache-Control: no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Expires: Fri, 01 Jan 1990 00:00:00 GMT, Date: Thu, 21 Feb 2013 11:39:04 GMT, Content-Type: application/json, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Transfer-Encoding: chunked]
{
  "error" : "invalid_request"
}

Is there any way to know what exactly caused the error? 有什么办法知道导致错误的确切原因吗?
Or could you find any fault with the request? 还是您发现请求有任何问题?

Though I don't have personal experience with the Apache library, it looks like that the parameters are sent as query parameters instead of posted form parameters. 尽管我对Apache库没有任何经验,但看起来参数是作为查询参数发送的,而不是表单形式的参数。 According to this HttpClient Quick Start , it should look like: 根据此HttpClient快速入门 ,它应类似于:

HttpPost httpPost = new HttpPost(urlString);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("code", code));
nvps.add(new BasicNameValuePair("client_id", client_id));
nvps.add(new BasicNameValuePair("client_secret", client_secret));
nvps.add(new BasicNameValuePair("redirect_uri", redirect_uri));
nvps.add(new BasicNameValuePair("grant_type", grant_type));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpPost);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” 用于获取访问令牌的Google oauth java客户端失败,并显示“400 Bad Request {”error“:”invalid_request“}” - Google oauth java client to get an access token fails with “400 Bad Request { ”error“ : ”invalid_request“ }” Google Places API,添加位置始终返回INVALID_REQUEST - Google Places API, Adding a place always returns INVALID_REQUEST 如何将OAuth 2.0授权代码交换为访问令牌? - How do I exchange a OAuth 2.0 authorization code for an access token? Google OAuth Java的访问令牌和ID令牌的交换代码 - Exchange code for access token and id token for google oauth java 无需用户交互的 Google Cloud OAuth 2.0 请求令牌 - Java - Google Cloud OAuth 2.0 Request Token without user interaction - Java 如何通过Google GData API请求OAuth 2.0访问令牌? - How to request a OAuth 2.0 access token by Google GData API? 为什么在执行 Exact OAuth 时会出现 OAuthProblemException error='invalid_request' description='Handle could not be extract' - Why do I get OAuthProblemException error='invalid_request' description='Handle could not be extracted' when doing Exact OAuth 添加位置后,Google Places POST“ INVALID_REQUEST”响应 - Google Places POST “INVALID_REQUEST” response when adding a place 谷歌日历 API Java 400 invalid_request - Google Calendar API Java 400 invalid_request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM