Google Calendar API – Creating a new Calendar with ColdFusion
Now that we have finished with the crazy method of obtaining all the various tokens for accessing the Google API, let’s jump into our first Google Calendar API method. We will start by using the Google Calendar API to create a new calendar. This method is very straight forward. It’s just a matter of correctly formatting the XML and sending a post request with the packet to the Google Calender API.
<cfargument name="token"/>
<cfargument name="gsessionid"/>
<cfargument name="title"/>
<cfargument name="summary"/>
<cfargument name="timezone" default=""/>
<cfargument name="color" default="##2952A3"/>
<cfset var atomxml="">
<cfset var filelist="">
<cfsavecontent variable="atomxml">
<cfoutput>
<?xml version='1.0'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gCal='http://schemas.google.com/gCal/2005'>
<title type='text'>#arguments.title#</title>
<summary type='text'>#arguments.summary#</summary>
<cfif len(arguments.timezone)><gCal:timezone value='#arguments.timezone#'></gCal:timezone></cfif>
<gCal:hidden value='false'></gCal:hidden>
<gCal:accesslevel value='owner'/>
<gCal:color value='#arguments.color#'></gCal:color>
</entry>
</cfoutput>
</cfsavecontent>
<cfhttp url="http://www.google.com/calendar/feeds/default/owncalendars/full?gsessionid=#arguments.gsessionid#" method="post" redirect=false>
<cfhttpparam type="header" name="Authorization" value="AuthSub token=#arguments.token#">
<cfhttpparam type="header" name="Content-Type" value="application/atom+xml">
<cfhttpparam type="body" value="#trim(atomxml)#">
</cfhttp>
<cfif isxml(cfhttp.filecontent)>
<cfxml variable="filelist"><cfoutput>#cfhttp.filecontent#</cfoutput></cfxml>
<cfreturn filelist/>
<cfelse>
<cfdump var="#cfhttp#">
<cfthrow detail="xml parsing error">
</cfif>
</cffunction>
The first two arguments I would hope you recognize by now. The “token” argument is the AuthSubSessionToken explained here, remember it’s the second token, not the first. The gSessionId is explained here.
The next 4 arguments: title, summary, timezone and color are all used for the XML packet we’ll send to the Google Calendar API. Title and summary are self explanatory, timezone is actually a non-standard text string. If you search Google you’ll be able to find a list of the various strings. I’m in the EST timezone but my Google timezone is “America/New_York”. Finally the color argument is simply a HEX string for the color you want the calendar to show up as in the Google UI.
We take those arguments and create an atom xml packet with the values and we send the packet to the “owncalendars” feed. Notice on the end of the URL is the gSessionId. If you don’t include that Google will give you a 403 error. Next notice that the AuthSub token is in cfhttpparam. You’re going to see that in every cfhttp request we make from now on. Finally take a look at the last cfhttpparam. That’s the atom+xml packet we just created. You’ll notice it’s a type=”body” which is likely another cfhttpparam you’ve never used.
Google will either respond correctly with an xml packet or something got messed up and it’ll throw an error. If they send back a correct xml packet, we parse it and return the parsed XML data. Otherwise we throw an error.
So that’s it. If you haven’t already, create a GoogleCalendar.cfc file and add the function above to it and try it out for yourself. Also make another file called TEST_googleCalendar.cfm and add this to it, change the token and gSessionId:
<cfinvokeargument name="token" value="CPPpjYXkBxCA64je%5Fv%5D%5F%4F%5F8B%0B"> <!--- Use the GoogleAuthenticate.cfc to get this value --->
<cfinvokeargument name="gsessionid" value="K%6DF9QBlomks"> <!--- Use the GoogleAuthenticate.cfc to get this value --->
<cfinvokeargument name="title" value="Test Calendar"/>
<cfinvokeargument name="summary" value="This is my first CF based calendar"/>
<cfinvokeargument name="timezone" value="America/New_York"/>
</cfinvoke>
<cfdump var="#newCalendar#">
Give that a shot and tell me if you have any problems with it.
Good luck!
-Steve Nelson

By the way, in the test file, don’t forget to change the token and gsessionid values. I named the token poorly. I should have named it: AuthSubSessionToken I’ll do that in the cfc I release.
If you get this to work, please let me know. I’m curious if anyone is actually following along.
Comment by Steve Nelson — January 31, 2008 @ 12:00 am
When and where are you going to post the finished CFC’s ? Thanks
Comment by Steve Julian — May 7, 2008 @ 12:00 am
I hope you’re still monitoring this…
When I post the request to Google I’m getting a 200 OK status (not an error) with an XML packet matching what you would get if you just requested a list of calendars, and the calendar is not being created.
Here’s what I’m doing (hope this doesn’t get mangled):
<cfsavecontent variable="calendarXML">
<entry xmlns=’http://www.w3.org/2005/Atom’
xmlns:gd=’http://schemas.google.com/g/2005′
xmlns:gCal=’http://schemas.google.com/gCal/2005′>
<title type=’text’>Test Calendar 3</title>
<summary type=’text’>Calendar Description</summary>
<gCal:timezone value=’America/Los_Angeles’></gCal:timezone>
<gCal:hidden value=’false’></gCal:hidden>
<gCal:color value=’#2952A3′></gCal:color>
<gCal:accesslevel value=’owner’/>
</entry>
</cfsavecontent>
<cfoutput>
<cfhttp url="http://www.google.com/calendar/feeds/default/owncalendars/full" method="post" redirect=true>
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#test.auth#">
<cfhttpparam type="header" name="Content-Type" value="application/atom+xml">
<cfhttpparam type="body" value="#trim(calendarXML)#">
</cfhttp>
<cfif find("302", cfhttp.statusCode)>
<cfset gsessionid = listlast(cfhttp.responseheader.location, "=")>
** Got 302 redirect **<br>
<cfhttp url="http://www.google.com/calendar/feeds/default/owncalendars/full?gsessionid=#gsessionid#" method="post" redirect=false>
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#test.auth#">
<cfhttpparam type="header" name="Content-Type" value="application/atom+xml">
<cfhttpparam type="body" value="#trim(calendarXML)#">
</cfhttp>
</cfif>
</cfoutput>
Any help would be appreciated
Comment by Clif — October 10, 2008 @ 12:00 am
Sorry for the 8 month delay.
I’ve been loaded with work. If you’re willing to try and figure some of it out yourself, shoot me an email and I’ll send the cfcs to you. snelson (at) webapper.com
Comment by Steve Nelson — October 10, 2008 @ 12:00 am