1

I am trying to make a HttpPost using the HttpClient(HttpComponents). This is my code:

try{
            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/json");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("code", new String(code.getBytes(), Charset.forName("UTF-8"))));
            nvps.add(new BasicNameValuePair("client_id", "googleClientId"));
            nvps.add(new BasicNameValuePair("redirect_uri", "http://localhost:8080/myapp/test"));
            nvps.add(new BasicNameValuePair("client_secret", "ClientSecret"));
            nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
            post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


            HttpResponse resp = httpClient.execute(post);

            if (resp.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                   + resp.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(
                             new InputStreamReader((resp.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
                res = res + output;
            }
            System.out.println(output);
            httpClient.getConnectionManager().shutdown();
            } catch(Exception e){
                e.printStackTrace();
            }
            System.out.println(res);

I get a 400 code. I suspect it is because all the parameters I set contain special characters, like _ . and / and they are getting escaped. How can I avoid this? Or am I missing the real problem?

Dragos
  • 2,803
  • 8
  • 34
  • 53
  • http://stackoverflow.com/questions/3286067/url-encoding-in-android – kosa Oct 15 '12 at 20:21
  • @Nambari This is Post, so I have nothing to do with the url. – Dragos Oct 15 '12 at 20:44
  • @Nambari What am I missing? Am I sending my parameters in the URL somehow? – Dragos Oct 15 '12 at 21:40
  • It seems you are. Could you just add one by one and narrow down which param could be causing the issue/. – kosa Oct 15 '12 at 21:43
  • @Nambari Apparently I was wrong. It's `redirect_uri`. Do you have any experience with OAuth2? – Dragos Oct 15 '12 at 22:05
  • @Nambari I have finally solved it! This helped me: http://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch Thank you! – Dragos Oct 15 '12 at 22:13

1 Answers1

1

Shouldnt http header "Content-Type" be application/x-www-form-urlencoded since you are explicitly URL encoding the post parameters?

javarebel
  • 150
  • 1
  • 9
  • Am I encoding the parameters in the URL??? Aren't they transmitted in the Post body? – Dragos Oct 15 '12 at 20:43
  • If it is so, how can I transmit the parameters in the request body? – Dragos Oct 15 '12 at 21:39
  • I finally solved it. This was only part of the problem, the rest was solved with according to the answer here: http://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch Thank you! – Dragos Oct 15 '12 at 22:15
  • Youre welcome. Please vote for my answer if you think it helped you in any way. Thank you. – javarebel Oct 15 '12 at 22:22
  • I will also accept the answer, since it solved part of the problem – Dragos Oct 15 '12 at 22:24
  • Thank you. Hopefully it will help somebody else in future with a similar problem. – javarebel Oct 15 '12 at 22:29