How to pull enterprise events from Box.com?

339 views Asked by At

Here's the sample code that I am trying to pull enterprise events using Box API-

TimeZone tz = TimeZone.getTimeZone("UTC");
Calendar start = Calendar.getInstance(tz);
start.add(Calendar.MONTH, -1); // retrieve events for last one month
Calendar end = Calendar.getInstance(tz);

try {
    EventLog el = EventLog.getEnterpriseEvents(api, start.getTime(), end.getTime(), BoxEvent.Type.values());
    System.out.println("Total- " + el.getSize());
    for (Iterator<BoxEvent> iterator = el.iterator(); iterator.hasNext();) {
        System.out.println(iterator.next().toString());
    }
} catch (BoxAPIException ex) {
    ex.printStackTrace();
    System.out.println(ex.getResponse());
}

And the actual error that I am getting-

{"type":"error","status":400,"code":"bad_request","help_url":"http://developers.box.com/docs/#errors","message":"created_after is beyond one year in the past, API only supports time range starting from one year in the past from now","request_id":"310879445579d0cc0fd33"}

2

There are 2 answers

4
Pâris Douady On

The error is that "start" seems to be too old (at least 1 year old) and causes a 400 error, because the API don't support it.

0
Sudhanshu Umalkar On

The issue seems to be in new Box Java SDK 1.0.0 — had to update com.box.sdk.EventLog:

    String afterString = BoxDateFormat.format(after);
    String beforeString = BoxDateFormat.format(before);

    // this one added by me to encode dates
        try {
            afterString = URLEncoder.encode(afterString, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            // what do we do here?
        }
        try {
            beforeString = URLEncoder.encode(beforeString, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            // what do we do here?
        }

        URL url = ENTERPRISE_EVENT_URL_TEMPLATE.build(api.getBaseURL(), afterString, beforeString);