Last week I demonstrated some techniques you can use to parse blog feeds produced by the AOL Open Blog API. While you can take advantage of the public capabilities of this API very easily, it's not all that hard to unlock its complete set of features either. This post will demonstrate how to work with the advanced features of AOL Journals through the authentication capability of the AOL OpenAuth API.
While the focus of this post is managing an AOL Journal, the incorporation of the OpenAuth features used to produce these results is just as important. As you may recall, I talked about using the OpenAuth API to manage the security framework of a ColdFusion application and have since added this capability to the AOL API project on RIAForge. I'll be extending this example to include a page which will manage a private AOL Journal I have started for myself.
To get started with this example, the most important thing to know is that I am using an OpenAuth ColdFusion component instance stored in session memory to manage my authentication scheme. For a complete reference on the methods and properties available with this component, check out the documentation contained in the AOL API project page. Since I have my security framework already configured with a few lines of code using OpenAuth, I can simply create a new page within my application directory and just start coding.
Reading Blog Information
By default, you are able to access and read all public information in the Open Blog API. However this is just a small portion of the capability you can use. To unlock the more advanced features of AOL blogging, you'll need to provide a few details to each request such as your AOL developer ID, an authentication token, and in many cases a referer value. We'll start by using the code below to obtain a collection of the blogs for my screenname.
<!--- get the workspace ---> <cfhttp url="http://api.blogs.aol.com/bricemason/service.xml" method="get"> <cfhttpparam type="header" name="Authorization" value="OpenAuth token=""#session.obj_openAuth.getAuthToken()#""" /> <cfhttpparam type="header" name="X-AOL-DEVID" value="#session.obj_openAuth.getDevId()#" /> <cfhttpparam type="header" name="referer" value="http://localhost:8501/openauth/" /> </cfhttp> <!--- parse the response into an XML data type ---> <cfset xml_blogWorkspace = xmlParse( cfhttp.FileContent ) /> <!--- get the location of the first collection ---> <cfset collectionUrl = xml_blogWorkspace.service.workspace.collection.xmlAttributes.href />
The first few lines configure the HTTP request used to retrieve the collection. The Authorization and X-AOL-DEVID headers are populated using the method calls available from the OpenAuth CFC from the RIAForge project. The response returned from this request is parsed into an XML data type and the first collection link is set in a variable named collectionURL. It's already quite clear that the OpenAuth CFC enables us to leverage the advanced capabilities of other AOL services (in this case, reading private collection data). Now that we have a link to a private collection, we can interrogate it further to reveal all the entries by using the code below.
<!--- get all the entries for the collection ---> <cfhttp url="#collectionUrl#" method="get"> <cfhttpparam type="header" name="Authorization" value="OpenAuth token=""#session.obj_openAuth.getAuthToken()#""" /> <cfhttpparam type="header" name="X-AOL-DEVID" value="#session.obj_openAuth.getDevId()#" /> <cfhttpparam type="header" name="referer" value="http://localhost:8501/openauth/" /> </cfhttp> <cfset xml_entries = xmlParse( cfhttp.FileContent ) /> <!--- get the blog entries ---> <cfset arr_entries = xmlSearch( xml_entries, "//:entry" ) /> <!--- output the basic information for each entry ---> <cfloop from="1" to="#ArrayLen( arr_entries )#" index="i"> <cfoutput> <h3>#arr_entries[i].title.xmlText#</h3> <cfset arr_link = xmlSearch( arr_entries[i], "./:link[ @rel = 'edit' ]" ) /> <a href="#cgi.script_name#?action=delete&editURI=#URLEncodedFormat( arr_link[1].xmlAttributes.href )#"> Delete </a> <p> #arr_entries[i].content.xmlText# </p> </cfoutput> </cfloop>
In much the same fashion as before, we prepare and execute a request for the collection using the path specified in the collectionUrl variable. The authentication properties are bundled in as custom headers and the response is parsed as XML. An XPath statement is used to gather an array of entry nodes which are then looped over, with some key properties such as title and content output. The other interesting piece of information is the link which contains the path for the editURI. This is the path which is used to delete the particular entry tied to the link. We simply run another XPath statement to find this link and create an <a /> tag to perform the actual delete request. Some example output of this code is as follows:
Each entry contains a title, a delete control in the form of a link, and the content. The following code is used to process the delete request.
<cfif isDefined( "url.action" ) and url.action eq "delete">
<cfhttp url="#url.editURI#" method="delete">
<cfhttpparam type="header" name="Authorization"
value="OpenAuth token=""#session.obj_openAuth.getAuthToken()#""" />
<cfhttpparam type="header" name="X-AOL-DEVID" value="#session.obj_openAuth.getDevId()#" />
<cfhttpparam type="header" name="referer" value="http://localhost:8501/openauth/" />
</cfhttp>
<!--- redirect back to the main page --->
<cflocation url="#cgi.script_name#" addtoken="no" />
</cfif>
This code again prepares the same type of request. Since authentication is required for the delete operation, we pass the token and AOL developer ID, however we specify the delete HTTP method. The delete operation is enacted through the use of querystring parameters used to specify where to send the request.
Creating a Blog Entry
So far we've been able to read information about private blogs and delete its entries. Now we'll walk through the process of creating new entries using our own application which leverages the capabilities offered by the Open Blog and OpenAuth APIs. First, create the following form:
<!--- provide a form to post a new entry --->
<cfform action="#cgi.script_name#?action=create&collectionUrl=#URLEncodedFormat( collectionUrl )#" method="post">
Title: <input type="text" name="title" value="" /><br/>
Content:<br/>
<textarea name="content" cols="35" rows="5"></textarea><br/>
<input type="submit" value="Create New Entry" />
</cfform>
The most important part of this code is the action attribute of the cfform tag. Here we're re-using the collectionUrl variable obtained earlier as this is the path to the collection we need to POST a new entry to. Finally the logic used to process this request is below:
<cfif isDefined( "url.action" ) and url.action eq "create">
<!--- configure the information to be posted --->
<cfxml variable="xml_newEntry">
<cfoutput>
<?xml version="1.0" encoding="utf-8" ?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:aj="http://schema.blogs.aol.com/_atom/aj##">
<title type="text">#form.title#</title>
<content type="html">#form.content#</content>
</entry>
</cfoutput>
</cfxml>
<!--- post the new entry --->
<cfhttp url="#url.collectionUrl#" method="post">
<cfhttpparam type="header" name="Authorization"
value="OpenAuth token=""#session.obj_openAuth.getAuthToken()#""" />
<cfhttpparam type="header" name="X-AOL-DEVID" value="#session.obj_openAuth.getDevId()#" />
<cfhttpparam type="header" name="referer" value="http://localhost:8501/openauth/" />
<cfhttpparam type="body" value="#xml_newEntry#" />
</cfhttp>
<!--- back to the main page --->
<cflocation url="#cgi.script_name#" addtoken="no" />
</cfif>
Here we configure the information to be posted as XML using the title and content passed in. The XML content is then POSTed to the path specified in the collectionUrl querystring parameter.
Just by walking through the three examples here which require the authentication features of the OpenAuth API, you can see how much sense it makes to encapsulate these common routines into their own AOL Journals component. Check back next week when I introduce this capability into the AOL API CFC project. For more information on the topics covered in this post, please visit the following resources:
- AOL OpenAuth API
- AOL Journals - Open Blog API
- iPhone Journal - Mobile Blogging From Your iPhone on AOL Journal - Part 1 - Good tutorial on how to start an AOL Journal
- Integrating OpenAuth into ColdFusion Applications
- OpenAuth Added to AOL CFC Project
- Parsing AOL Blog Feeds with ColdFusion
- AOL API CFC Project - RIAForge

thank you
but so difficult for me :'(