I am trying to send a POST request to https://test.salesforce.com/services/oauth2/token to retrieve the oauth access token. The response is 400 Bad Request with no info. Here's the (Java) client code:
private void authenticate() {
RestTemplate req = new RestTemplate();
JSONObject oauth_req = new JSONObject();
//JSONObject oauth_res = new JSONObject();
oauth_req.put("grant_type", "password");
oauth_req.put("client_id", this.CONSUMER_KEY);
oauth_req.put("client_secret", this.CONSUMER_SECRET);
oauth_req.put("username", this.USER);
oauth_req.put("password", this.PASSWORD);
String reqToString = oauth_req.toString();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(reqToString,headers);
String res = req.postForObject(TEST_AUTH_URL, entity, String.class);
System.out.println(res);
}
I have attempted this with both postForObject() and exchange() with the same results. I am certain that the body is correct, as I am able to retrieve the token with Postman. Is it possible that the issue is with my header or entity?
application/x-www-form-urlencoded
format not in JSON format.