Tuesday, January 20, 2009

Creating an application to insert events into Google Calendar

Here is how you can create a quick command-line application in python that will post events into Google Calendar.
  1. Get a Google Calendar account (go here to get one).
  2. Install the python gdata client which you can get from here.
  3. Find out the url to use to post to your calendars. To do that just go to this url and then view the source of the xml document that it returns to you. Each of your google calendars will be listed with the url that you need to use for each one. Remove the 'http://www.google.com' part of the url when you use it in your code.
  4. Write up some code to post with. Here is an example.
import gdata.calendar.service
import gdata.calendar
import atom

def add_to_gcal(item):
cal_client = gdata.calendar.service.CalendarService()
cal_client.email = 'youremail@gmail.com'
cal_client.password = 'yourpassword'
cal_client.ProgrammaticLogin()

event = gdata.calendar.CalendarEventEntry()
event.content = atom.Content(text=item)
event.quick_add = gdata.calendar.QuickAdd(value='true');

new_event = cal_client.InsertEvent(event, 'url from step 3 above')

add_to_gcal('Dentist Appt today at 4pm')
print "Your event was created"
There you have it.

3 comments:

unsquander said...

Well this was an awesome tutorial. Thanks!

rehidden said...

Hi Paul,
I am trying to get my app with your code example but I am getting a 401 error, do I have to enable sharing on my calendar to get your code working?

Sze

rehidden said...

Many thanks Paul for your python code.
I was able to get it working and realized that when I run your code on Google app engine, I have to add:
gdata.alt.appengine.run_on_appengine(cal_client) before cal_client.ProgrammaticLogin()

Sze