Events API endpoint help

I’m trying to schedule a job which runs periodically and queries the events API. I have everything up and running but I just don’t get any data returned. Here is what happens.

I query for all unarchived projects for our customer, this returns successfully. I iterate over the projects and call the endpoint with the project id, i.e. /events?resource=14321. I receive a 412 response code and get the sync token from the response. This token always seems to end with a “:0”. I stored the token and about half an hour later, I run the same query but this time I’m including the sync token, i.e. /events?resource=14321&sync=1231232313:0. I’m always getting a 200 response without any data, something like
{“data”:,“sync”:“1231232313:1”}.
Since the sync token has changed, I store the new token and the cycle continues from there, with the token incrementing after every call for the projects events endpoint. The problem I’m having is that this code has been running for 24 hours and every project returns an empty data array, and I know people are doing things in the application. Am I doing something wrong here? There don’t seem to be errors other than the expected 412 the first time I call this code.

I also tried doing the same thing for tasks, and the same empty data array is being returned.

I am not answering your question but did you consider using the official libraries to work with Asana API? That might be a little bit easier for you.

No, I’m using Java and the client libs don’t work. When calling the OAuthApp constructor and including a refresh token, I get an exception because the Credential object is being constructed wrong in the lib. I can get around this by building my own Credential object and setting it in the OAuth app. But even after doing that, the code just does endless polling of the endpoint. I get back no results, and a new sync token with every request. It’s coming from this code in EventsPageIterator:

while (true) {
Collection result = super.next();
if (result.size() > 0) {
return result;
} else {
try {
Thread.sleep((Integer) request.options.get(“poll_interval”) * 1000);
} catch (InterruptedException e) {
}
}
}

@Jeff_Schneider @Matt_Bramlage @johnnygoodnow are you aware of any bug with the Java library. ?

The specific location of the error is in the setRefreshToken method in Credential.java (in the google-oauth-client lib). When this is called in the constructor of OAuthApp.java, it is returning this error message “Please use the Builder and call setJsonFactory, setTransport, setClientAuthentication and setTokenServerUrl/setTokenServerEncodedUrl”. An easy fix is to build the Credential object with the builder and then set the tokens as they say.

credential = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
.setJsonFactory(GsonFactory.getDefaultInstance())
.setTransport(new NetHttpTransport())
.setClientAuthentication(new ClientParametersAuthentication(
clientId,
secretId))
.setTokenServerUrl(new GenericUrl(TOKEN_SERVER_URL))
.build();
credential.setAccessToken(accessToken);
credential.setRefreshToken(refreshToken);

As far as the endless polling, it’s not really a bug, but endlessly polling an endpoint in a while loop until you get some results back certainly seems odd to me though. I’d rather just get back an empty result set and a new sync token that I can use later. If there are no results, there are no results. No need to have a method that might never return that endlessly hits an endpoint. But yeah, the Java libs don’t work for my case.

Hi @Jason_feist,

Would you mind filing the OAuth issue as a bug under our Java client library on Github? We do realize there are some bugs in the Java client library that are more prevalent than our other libraries, and we’re actively working on making our client libraries better. That being said, apologies that it’s not working for your use case.

As for the events API, have you tried a simple test case on a test project? How often are you polling? If it’s over 24 hours, the token may have expired - in which case you’d get no events (because you’d be fetching a new token at that point, in which no events have happened); not sure if you saw that in our docs.

Could you manually try the following:

  • Create a test project in Asana with no tasks
  • Query for the events for that project, expecting a 412, and store the sync token.
  • Create some new tasks in the project
  • Query for events in that project, expecting a 200 and some events corresponding to your actions.

Another option is to verify this using cURL - does this issue still persist?

If the simple test case in Java does not work, can you write out that sample code so that we could further help you debug the issue?

1 Like

Thanks for the reply. It looks like the OAuth issue was already created (IllegalArgumentException from OAuthApp when passing in tokens · Issue #41 · Asana/java-asana · GitHub). I’m not sure what happened, but my code has started returning results. There was an outage the day before I started testing this so maybe that affected things? I’m going to take your advice and set up a new test project and see what happens. I’m confident this code is set up properly though because it’s worked before.

1 Like