We're speaking at
CFUnited 2008:
CFUnited - The Premiere ColdFusion Technical Conference

Search

Calendar

SunMonTueWedThuFriSat
    123
45678910
11121314151617
18192021222324
25262728293031

Subscribe Enter your email address to subscribe to this blog. You'll receive an email when we write a new post.

Recent Entries Come On In, Rails-The Water's Warm
Shan's Simple Examples: File uploads with Flex and ColdFusion

Recent Comments Google Calendar API - Creating a new Calendar with ColdFusion
Steve Julian said: When and where are you going to post the finished CFC's ? Thanks [more]

Three Phases of Programmer Development
Pat Branley said: I normally think of those phase 2 people as 'programmers' and the phase 3 people as 'developers'. I... [more]

New Job Title: Front End Engineer
Sean Corfield said: Well, there's always the excellent Fusion Authority Quarterly Journal... [more]

Down To The Wire: HTTP Sniffers
Brian M said: I second the mention of the Charles Web Debugging Proxy that Tariq mentioned. It is fantastic. It s... [more]

New Job Title: Front End Engineer
Patrick said: Heya Sean. Good point. I never understood how they did things over there at SysCon, and I understand... [more]

Archives By Subject Business of Software (4) [RSS]
ColdFusion (318) [RSS]
Conferences (6) [RSS]
Databases (87) [RSS]
Flex & Flash (109) [RSS]
Fusebox (87) [RSS]
General Development (29) [RSS]
Google (9) [RSS]
Hardware (5) [RSS]
JVM & Java (132) [RSS]
Linux (20) [RSS]
Miscellaneous (254) [RSS]
Performance (8) [RSS]
SeeFusion (36) [RSS]
Shan's Simple Examples (7) [RSS]
User Interface (3) [RSS]
Windows (5) [RSS]

Archives By Poster Daryl Banttari (10)
Nat Papovich (29)
Patrick Quinn (36)
Shannon Hicks (22)
Steve Nelson (21)
Tyson Vanek (3)


bottom corner

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.

<cffunction name="newCalendar" >
   <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:

<cfinvoke component="GoogleCalendar" method="newCalendar" returnvariable="newCalendar" >
   
<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

Comments
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. :-)
# Posted By Steve Nelson | 1/31/08 7:22 AM
When and where are you going to post the finished CFC's ? Thanks
# Posted By Steve Julian | 5/7/08 9:19 AM

bottom corner