Tell me why? Google and CalDAV
Back in 1999 the Backstreet Boys already knew
Tell me whyAin’t nothin’ but a heartacheTell me whyAin’t nothin’ but a mistake
That reflects pretty well the feeling you get when trying to use the Google CalDAV API over their own Google Calendar API.
This article explains how to use the CalDAV API with Python nevertheless.
Resources and Documentation
Unfortunately there is lack of documentation regarding the integration of Google calendar using the CalDAV interface. Plenty of information is available for their own Calendar API however.
The only resource I found using CalDAV and showing code examples was Fetching google calendar events with python which is unavailable at the time I’m writing this blog post. Below is a quote with the important parts (the Python code) of the linked article.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#!/usr/bin/python import datetime import caldav from caldav.elements import dav, cdav username = "myusername" password = "mypassword" calendarname = "Maintainance" calenderid = "something@group.calendar.google.com" # OK, lets go url = "https://" + username + ":" + password + "@www.google.com/calendar/dav/" + calenderid + "/events" # Setup Client client = caldav.DAVClient(url) # Fetch calendar cal = client.principal().calendar(name=calenderid) # Fetch todays events events = cal.date_search(datetime.date.today(), datetime.date.today() + datetime.timedelta(days=1)) # Get the events and push them to stdout for event in events: event.load() e = event.instance.vevent print "(" + e.dtstart.value.strftime("%H:%M") + ") " + e.summary.value
Sadly or maybe thankfully time went on and it is now no longer possible to use the API with just the credentials. An OAuth authorization is required.
OAuth
Fortunately the Calendar API example describes how to get such an OAuth token to work with their calendar interface:
- Go to the Google Developer Console.
- Create a new project and select that project.
- Enable CalDAV API (opposed to the Calendar API which is used in the linked example).
- Setup OAuth Client ID (and configure OAuth consent screen).
- Download OAuth Client ID to file
credentials.json
The authentication code can be taken from the example. The scope has to be read/write however, otherwise the CalDAV client can’t successfully load:
|
|
python-caldav
Now the DAVClient from the caldav
Python package has to use the credentials
to authenticate. To achieve that we
write a small authenticator.
|
|
The credentials are passed to the authenticator where calid
is the calendar
ID. The specific ID can be retrieved by going to the Google calendar settings
for the specific calendar then searching for “Calendar ID”.
|
|
Full Code
Package requirements:
google-auth-oauthlib
caldav
The following code will initiate the Google OAuth process based on the
credentials.json
downloaded to the same directory and then print all
birthday events from the associated contacts.
|
|
Conclusion
With the example provided in this article it is possible to use the Google CalDAV API. However the v2 API is used where v3 is the current version. Therefore it remains to be seen for how long CalDAV stays supported.
A wise man would probably use the well-documented v3 interface. The Backstreet Boys would too.