title: Getting 400 BAD Request when using Spring RestTemplate to POST
body: <p>Can someone please help me figure out what is wrong with the code below?</p>
  
  <p>I am using <strong>Spring 3.1.1 RestTemplate</strong> to try to call a <strong>REST WS on Box.com</strong> to get a new access token from a refresh token. </p>
  
  <p>The code below is returning a <code>400 (BAD REQUEST)</code>. I am able to <em>successfully</em> call the same method using the <strong>FireFox POST plugin</strong>. I've compared output from the <code>writeForm method</code> on the <code>FormHttpMessageConverter class</code> and it is exactly as I am sending it from FireFox.</p>
  
  <p>Does anyone have any ideas?</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException {
      try {
          String apiUrl = "https://www.box.com/api/oauth2/token";
          String clientSecret = "[MY SECRET]";
          String clientId = "[MY ID]";
          String currentRefreshToken = "[MY CURRENT VALID REFRESHTOKEN]";
  
          RestTemplate restTemplate = new RestTemplate();
  
          List&lt;HttpMessageConverter&lt;?&gt;&gt; messageConverters = new ArrayList&lt;HttpMessageConverter&lt;?&gt;&gt;();
  
          messageConverters.add(new FormHttpMessageConverter());
          restTemplate.setMessageConverters(messageConverters);
  
          MultiValueMap&lt;String, String&gt; body = new LinkedMultiValueMap&lt;String, String&gt;();
  
          body.add("grant_type", "refresh_token");
          body.add("refresh_token", currentRefreshToken);
          body.add("client_id", clientId);
          body.add("client_secret", clientSecret);
  
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
          headers.add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json");
          headers.add("Accept-Encoding", "gzip, deflate");
  
  
          HttpEntity&lt;?&gt; entity = new HttpEntity&lt;Object&gt;(body, headers);
  
          restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
      } catch (Exception ex) {
          System.out.println("ex = " + ex.getMessage());
      }
    }
  }
  </code></pre>