0

I have successfully got the authorization_code. And getting "Error" : "invalid_request" in response for authorization_code-token exchange.
Here is my java code to get the Google OAuth token in exchange to authentication_code:
(Made use of HttpComponents for HTTP requests)

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?

1
  • Looks pretty good to me. Any way to capture the actual HTTP post traffic? 400 in my experience means a syntax error. Your code doesn't include how you use urlString to make "post" but I'm assuming that's a no-brainer; also don't see things like post.setHeader("Content-Type", "application/x-www-form-urlencoded"); Commented Feb 22, 2013 at 5:04

1 Answer 1

3

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. According to this HttpClient Quick Start, it should look like:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.