Event Sync Token (412 response status )

1.7k Views Asked by At

I'm using RestTemplate to make the GET call to asana's REST Api. By using postmen when i'm calling:

https://app.asana.com/api/1.0/events?resource=PROJECT_ID

I'm getting a message and a sync token ( this is the same case when the sync token is too old and needs to be renewed ).

By using the RestTemplate, when the sync token is too old \ its the first call i'm making and I need a sync token, I'm getting a 412 response "Prediction Faild". This happens also in postman, but i'm getting along with the "error" message the new sync token.

With the RestTemplate all i'm getting is this error:

Aug 06, 2015 3:56:55 PM org.springframework.web.client.RestTemplate handleResponseError WARNING: GET request foPROJECT_ID21650756795165" resulted in 412 (Precondition Failed); invoking error handler Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 412 Precondition Failed at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:494) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385) at availo.worker.asana.MainTask.getEvents(MainTask.java:86) at availo.worker.asana.MainTask.getProjects(MainTask.java:76) at availo.worker.asana.MainTask.main(MainTask.java:115)

Any suggestions?

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

This worked for me. It lets you read the message that comes with the error:

restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){ protected boolean hasError(HttpStatus statusCode) { return false; }});

2
On

When first subscribing to Events on a resource you will receive a 412 Precondition Failed response code due to the fact that there is no sync token established yet. You should extract that sync token and use it in your next request to begin receiving events.

It looks like RestTemplate is invoking an error handler due to the 412 response code, which is understandable as a 412 is an error code.

If you can override DefaultResponseErrorHandler.handleError(), check that the response is a 412, then extract the sync token you could pass that in your next request to get events on the resource.

Another option is to use our Java client library, which should handle all of this for you.

This is what it might look like using our client:

System.out.println("Watching for events on project: " + project.name);

for (Event event : client.events.get(project.id)) {
    System.out.println("User: " + event.user.name + "\nAction: " + event.action + "\nResource: " + event.resource);
}