Search code examples
springfacebookresttemplate

how to make a POST call to Facebook Marketing API?


I'm trying to do a POST call to Facebook's graph API from my application. The method's relevant excerpt is:

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization","Bearer "+ accessToken);

HttpEntity<EventSetVM> request = new HttpEntity<>(new EventSetVM(eventSetName), headers);

Map<String, String> params = new HashMap<>();
params.put("name", eventSetName);

String url = "https://graph.facebook.com/v2.8/" +
             businessManagerId +
             "/offline_conversion_data_sets";

EventSetVM eventSetVM = restTemplate.postForObject(url, request, EventSetVM.class, params);

The error I'm getting from Firefox's RESTClient is

{
    "timestamp": 1487779045470,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.web.client.HttpClientErrorException",
    "message": "400 Bad Request",
    "path": "<correct-path-with-no-parameters>"
}

What's going on here?


Solution

  • Graph API requires explicit "access_token" parameter in your request. You need to add your access_token parameter in your params.