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