7
votes

i'm using python urllib for making request on Google calendar(API V3).

Now,Problem is that when i make request for 'Events: list',then i got zero items in response although there are events in that calendar.

Example

   {  "kind": "calendar#events",
   "nextPageToken": "CigKGm83a292ZzZ2YXBsNXRsMHJhZnV2cGprdHVvGAEggIDA97TfuYYUGg0IABIAGOjEqd_6q7kC",
   "items": [  ] 
   }

If i use 'nextPageToken' in next request it works fine.(But i don't want make any Extra request.)

This problem not occur every time. If i create new email_id this works fine. but after one or two month this problem start again.

Is this a bug in Google Calendar API ?

or

Is there any solution to get event list of any calendar in just One Request ?

Thanks in advance.

4

4 Answers

12
votes

I've been trying to figure out the same issue and I believe I have the answer.

The issue here is that when Google calculates a 'page' of events to return, it includes the deleted events in that calculation, and your first page is full of deleted events that you don't see unless your request has "showDeleted = True".

I provide a way for the user to 'clear' their calendar and repopulate it and have run into this issue. Consider this scenario:

  • The user has 250 events in their calendar and for arguments sake, lets say Googles 'page' size the same size.

  • When the user runs the process to repopulate, these 250 events are removed and 250 'new' events are created.

  • When we next go to remove the events prior to a repopulate process, the first page returns with no events - this is because the first 250 in the list are those that were deleted originally. (I have verified this by using the API Explorer )

  • Using the nextPageToken to get the next page of results works - as you have noted.

  • This is why creating a new calendar works for a period of time - ie until you get over that 'page' limit and start returning 0 events, which is where we run into issues.

  • Over time and if the user uses this repopulate function a lot, their list of deleted events can become huge and it will require many requests to return all Events. (I know of no way to purge all the deleted events completely from Google Cal - they seem to remain forever)

  • There is no way I know of to return all events in one call. You need to loop through the process getting a page at a time until the "NextPageToken" is no longer returned. This makes sense, because for users that have huge calendars with 1000's of appointments, it's inefficient to return everything in one request.

Using the Google Calendar API V3 here is a sample of what I'm using in VB.Net to remove all Events. (Service is a Google.Apis.Calendar.v3.CalendarService)

    Private Sub ClearAllEvents()

        Dim pageToken As String = Nothing
        Dim events As Events
        Dim qry As New ListRequest(Service, GCalId)

        Do
            qry.PageToken = pageToken
            events = qry.Execute()
            For Each ev As [Event] In events.Items
                Dim dr As New DeleteRequest(Service, GCalId, ev.Id)
                dr.Execute()
            Next
            pageToken = events.NextPageToken
        Loop While Not IsNothing(pageToken)
    End Sub
1
votes

add showDeleted = false and pageToken=next_page_token to url and call the api again.

1
votes

I had a similar problem(Full description in gleng's answer). The only solution I found and I still use it now: Just add a parameter (singleEvents=true) to request. This will remove the empty responses from calendar API. Good luck.

0
votes

Here is the C# version

private void ClearAllEvents()
{
    string pageToken = null;
    Events events;
    EventsResource.ListRequest qry = service.Events.List(CALENDAR_ID);

    do
    {
        qry.PageToken = pageToken;
        events = qry.Execute();

        foreach(Event ev in events.Items)
        {
                service.Events.Delete(CALENDAR_ID, ev.Id).Execute();
         }
         pageToken = events.NextPageToken;
     } while (pageToken != null);
 }