1
votes

I'm making the same query to get Calendar events two different ways. The odd thing is, one way it works, the other way it doesn't, even though everything is nearly exactly the same. Using the timeMin, timeMax on Google's API Explorer returns bad request for the time that the second method is sending.

Not only that, but the Google Calendar API docs say the field should be start-min and start-max while the client and the API Explorer say the field should be timeMin and timeMax.

Here's the code for method 1 that works:

void setPeriod() {
    Date nowDate = new Date();
    startTime = new DateTime(nowDate);
    Time t = new Time();
    t.setToNow();
    t.hour = 23;
    t.minute = 59;
    nowDate.setTime(t.toMillis(false));
    endTime = new DateTime(nowDate);
}

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
    try {
    Events evlist = g.getClient().events().list(calendarIds.get(cal)).setTimeMin(startTime).setTimeMax(endTime).execute();
    if (evlist.getItems() != null) {
        //Parse the events
    ...
}.execute();

Method 2:

new AsyncTask<Void, Void, List<Event>>() {
    @Override
protected List<Event> doInBackground(Void... arg0) {
    Date nowDate = new Date();
    DateTime startTime = new DateTime(nowDate);
    Time t = new Time();
    t.setToNow();
    t.hour = 23;
    t.minute = 59;
    nowDate.setTime(t.toMillis(true));
    DateTime endTime = new DateTime(nowDate);
    Calendar client = com.google.api.services.calendar.Calendar
                .builder(transport, jsonFactory)
                .setApplicationName("Wayk by Senti")
                .setHttpRequestInitializer(
                    new HttpRequestInitializer() {
                        public void initialize(HttpRequest request)
                                        throws IOException {
                                    request.getHeaders()
                                            .setAuthorization(
                                                    GoogleHeaders
                                                            .getGoogleLoginValue(authToken));
                                }
                            })
                    .setJsonHttpRequestInitializer(
                            new JsonHttpRequestInitializer() {

                                public void initialize(
                                        JsonHttpRequest request)
                                        throws IOException {
                                    CalendarRequest calendarRequest = (CalendarRequest) request;
                                    calendarRequest
                                            .setKey(CalendarClientCredentials.KEY);
                                }
                            }).build();
            try {
                List<Event> events = new ArrayList<Event>();
                for (int i = currentCal; i < enables.size(); i++) {
                    if (enables.get(i)) {
                        AiLog.v("Getting events");
                        AiLog.v("AuthToken: " + authToken);
                        AiLog.v("CalendarID: " + calendarIds.get(i));
                        AiLog.v("Calendar name: " + calendarNames.get(i));
                        AiLog.v("Calendar account: " + account);
                        Calendar.Events.List eventReq = client.events()
                                .list(calendarIds.get(i)).setTimeMin(startTime).setTimeMax(endTime).execute();//Bad request here

Note that the client is exactly the same for both. The client in the second method successfully grabs the list of calendars for the account. I have verified that the calendar ID is correct, which leaves me with the error coming from startMin and startMax.

Any help appreciated.

Cheers, Z

1
It is an issue with the timeMin and timeMax fields. If you remove the min and max times, it works fine. I have been trying to solve the same problem with the latest libraries. The thing is in earlier versions (calendar api library v1.5.0), the timeMin and timeMax accepted string values so I could format the date according to the timezone but now it has been changed to accept a DateTime value and setting a DateTime value even using the constructor that accepts the timezone shift doesn't seem to work and produces the 400 Bad Request.praneetloke
I had it working in the older library too. I figured it out, though, finally. Check the answerZaid Daghestani

1 Answers

10
votes

Figured it out.

You have to use the DateTime constructor that accepts a TimeZone.

ie

DateTime startTime = new DateTime(new Date(), TimeZone.getDefault());

Hope this saves someone else the 16 hours I spent trying to figure this out. Really bad documentation.