title: Using HttpEntity and HttpHeaders to connect to SalesForce
body: <p>I'm completely new at this, so forgive me if this question seems really basic.</p>
  
  <p>I'm following this guide: <a href="https://developer.salesforce.com/docs/atlas.en-us.datadotcom_api_dev_guide.meta/datadotcom_api_dev_guide/quickstart_connectwithoauth.htm" rel="nofollow">https://developer.salesforce.com/docs/atlas.en-us.datadotcom_api_dev_guide.meta/datadotcom_api_dev_guide/quickstart_connectwithoauth.htm</a></p>
  
  <p>And I want to get a access token, but I keep getting 400 bad request. What am I doing wrong here?</p>
  
  <pre><code>String uri = "https://na1.salesforce.com/services/oauth2/token";
  
  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);
  headers.set("client_id", "some_id");
  headers.set("client_secret", "secret_thing");
  headers.set("grant_type", "password");
  headers.set("username", "username@foo.com");
  headers.set("password", "password_here");
  
  HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt; (headers);
  
  System.out.println(new RestTemplate().exchange(uri, HttpMethod.GET,request, Object.class));
  </code></pre>
  
  <p>What's wrong with the above code? I keep getting a 400 bad request instead of a json blob, which is really what I want.</p>
  
  <p>Here is the error I keep getting:</p>
  
  <pre><code> 15/03/22 12:55:35 WARN client.RestTemplate: GET request for "https://na1.salesforce.com/services/oauth2/token" resulted in 400 (Bad Request); invoking error handler
  
  org.springframework.web.client.HttpClientErrorException: 400 Bad Request
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
      at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:532)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:488)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:446)
      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:393)
  </code></pre>