Search code examples
spring-bootresttemplate

Spring Boot RestTemplate: Bad request when directly copying from postman


So I have an API request where I am copying the details directly from postman where it works. I am however getting a bad request error.

@Service
public class GraphApiService {

@Bean
public RestTemplate restTemplate() {

    return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@Autowired
Constants constants;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public ResponseEntity<String> getAccessTokenUsingRefreshToken(Credential cred) throws IOException{
    try {

    //https://learn.microsoft.com/en-us/graph/auth-v2-user
    // section 5. Use the refresh token to get a new access token
    String url = "url";
    JSONObject body = new JSONObject();
    body.put("grant_type", "refresh_token");
    body.put("client_id", "clientid");
    body.put("scope","User.Read offline_access Files.Read Mail.Read Sites.Read.All");
    body.put("redirect_uri", "http://localhost");
    body.put("client_secret","secret");
    body.put("refresh_token", "token");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<String> request =  new HttpEntity<String>(body.toString(), headers);
   ResponseEntity<String> response=  restTemplate.postForEntity(url, request,String.class);
   return response;
    }
    catch(HttpClientErrorException  e){
        logger.error(e.getResponseBodyAsString());
        logger.error(e.getMessage());
        return null;
    }

}

I would appreciate any help. The bad request error message from microsoft graph isn't a descriptive one that will help


Solution

  • You're sending JSON payload with FORM_URLENCODED header. Either you need to check if API accepts json payload, if so you need to change content-type to application/json or you can post form data as follows.

    public ResponseEntity<String> getAccessTokenUsingRefreshToken(Credential cred) throws IOException{
        try {
            //https://learn.microsoft.com/en-us/graph/auth-v2-user
            // section 5. Use the refresh token to get a new access token
            String url = "url";
            MultiValueMap<String, String> multiValueMap= new LinkedMultiValueMap<String, String>();
            multiValueMap.add("grant_type", "refresh_token");
            multiValueMap.add("client_id", "clientid");
            //.....
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> request =  new HttpEntity<>(multiValueMap, headers);
            ResponseEntity<String> response=  restTemplate.postForEntity(url, request, String.class);
            return response;
        }catch(HttpClientErrorException  e){
            logger.error(e.getResponseBodyAsString());
            logger.error(e.getMessage());
            return null;
        }
    
    }