Reading Google API documentation, it was very easy to retrieve events in a calendar, but I realized that there are a few exceptions:
1- Recurrent events: There are a bunch of questions regarding retrieving these evens in other posts. The solution that I found in other posts was to set singleEvents to true, but unfortunately in this case there will be problems with all-day events.
2- Usually each event should have an id, which is a unique code. When you retrieve recurrences of an event the id will have a pattern like "mainEventID_recurrenceTimestamp", but when you retrieve the list of events in a calendar, there should be no underscore in the event id. However sometimes when someone updates an event on your calendar, for example updating the location of the event, there will be two events with the same value for all the properties, and the only difference will be the updated property (location) and ids. The updated event will have an id like "mainEventID_recurrenceTimestamp". Two types of problems may happen in such a situation:
a) You will retrieve and process two instances of the same event. b) When you want to find all of the recurrent events in your first request, and retrieve all the recurrences in the following requests, and you pass the id with a pattern like "mainEventID_recurrenceTimestamp", there will be no response.
I spent some time to figure out these exceptions, and I wrote a couple of lines of JavaScript code which can solve these exceptions and retrieve all events from a calendar including recurring and all-day events.
I put my code here, so if anyone else encountered similar exceptions, they will be able to use this code. Also I will appreciate it if you improve this code, so others can benefit from it:
function retrieveAllEvents(yourcalendarId, yourKey) {
var events;
var waitForResponses = [];
var recievingData = false;
$.get( "https://www.googleapis.com/calendar/v3/calendars/" + yourcalendarId + "/events/?",
{ (Optional parameters like maxResults), key: yourKey } )
.done(function( data ) {
events = data["items"];
for (i = 0; i < events.length; i++) {
if (events[i]['status'] != "confirmed") {
events.splice(i, 1);
i--;
}
for (j = 0; j < events.length; j++) {
if (i != j && events[i]['id'].indexOf(events[j]['id']) > -1) {
events.splice(i, 1);
i--;
break;
}
}
}
var thereisRecurrentEvent = false;
for (i = 0; i < events.length; i++) {
if ("recurrence" in events[i]) {
waitForResponses.push(events[i]['id']);
thereisRecurrentEvent = true;
events.splice(i, 1);
i--;
recieveRecurrentEvent();
}
}
if (!thereisRecurrentEvent) {
AllEventsReceived(events);
}
});
function retrieveRecurrentEvent() {
if (!recievingData) {
recievingData = true;
$.get( "https://www.googleapis.com/calendar/v3/calendars/" + yourcalendarId + "/events/" + waitForResponses[0] + "/instances?",
{ (Optional parameters like maxResults), key: yourKey } )
.done(function( data ) {
events = events.concat(data["items"]);
for (j = 0; j < events.length; j++) {
if (events[j]['status'] != "confirmed") {
events.splice(j, 1);
j--;
}
}
waitForResponses.splice(0, 1);
if (waitForResponses.length == 0) {
AllEventsReceived(events);
}
else {
recievingData = false;
retrieveRecurrentEvent();
}
});
}
}
return events;
}