Using OACurl to explore Blogger’s API
Blogger’s GData API is now available over HTTPS using OAuth 2. These changes result in less code, increased productivity, and enhanced security. Let’s take a look at how it works using OACurl, a wrapper for the curl command line tool that authenticates you using OAuth.
Prerequisites
First up you need to install Mercurial and Maven. Mercurial is the source revision control software used by the OACurl project. Maven is a java build system used to download and include the dependencies required by OACurl. Once we begin using oacurl we will use HTML Tidy to make the returned XML readable.
Getting OACurl
Now that you have installed Maven and Mercurial, you can checkout oacurl and build it as follows.
$ hg clone https://oacurl.googlecode.com/hg/ oacurl
$ cd oacurl
$ mvn assembly:assembly
Once you have done this you will have a shell script in your current directory called oacurl that you can use to invokeoacurl with all the appropriate Java class paths set for you.
Logging into Blogger
Logging into Blogger using OAuth involves doing a round trip to Google’s servers to grant access for third parties. In the case of oacurl, this involves invoking a web browser that shows you that oacurl is requesting access to your Blogger account.
$ ./oacurl login --blogger
Once you have granted access, then you will be able to explore Blogger’s API using oacurl.
Retrieving a list of your Blogger blogs
A good place to start is to list out your blogs. This is an Atom feed where each item in the list is a blog owned by the user who requested it.
$ ./oacurl https://www.blogger.com/feeds/default/blogs
To see the blogs of a specific user you can change the default for a specific user’s profile id instead. For instance, here is a list of my blogs.
$ ./oacurl https://www.blogger.com/feeds/16258312240222542576/blogs
To make the output XML readable, I suggest piping the output from the commands through tidy like this:
$ ./oacurl https://www.blogger.com/feeds/default/blogs | tidy -xml -quiet -indent
I will leave that off the following commands for readability.
Retrieving the feed of a specific Blogger blog
To retrieve the content of a blogger blog, we select the blog we want from the list of blogs in the blog feed, and then follow the appropriate link. In this case we are using the post link as it stays within the scope of our OAuth authorisation.
type='application/atom+xml' href= 'http://www.blogger.com/feeds/4967929378133675647/posts/default' />
Posting a new entry to a feed
If I wanted to post a new entry to my test blog, all I need to do is create a new entry in Atom format, and post it to the above url. So, I create a file with the following content called post.xml:
I can submit this to Blogger to create a new post like this:
$ cat post.xml | ./oacurl -X POST https://www.blogger.com/feeds/4967929378133675647/posts/default
This, of course, won’t work for you, as my test blog is only set up for me to post to. Modify the post URL in the command to match the one from one of the Blogger blogs you own. The result from the command is the fully expanded Atom entry for the post that was just created.
This Atom entry contains the information you need to modify it. The link with rel='edit' is the magic entry. For blogger, this is the same as the rel='self' link. To edit the content for the blog post I just created, first I retrieve the content at the edit URL:
$ ./oacurl https://www.blogger.com/feeds/4967929378133675647/posts/default/2170959137511831372 > post-edit.xml
We can now modify the title and content using a text editor, and then PUT it back, in proper REST style.
$ vi post-edit.xml
$ cat post-edit.xml | ./oacurl -X PUT https://www.blogger.com/feeds/4967929378133675647/posts/default/2170959137511831372
The Atom representation also contains the information required to create, retrieve, and delete the comments on this entry. See if you can spot the appropriate URLs in the output XML.
Using the libraries
You can use various libraries with the Blogger GData APIs, for example the Java GData client, but it is very useful to understand what the library is doing for you under the covers. And exploring is fun!
If you have any questions after reading this post, please feel free to post your questions to the Blogger Dev Google Group.
References
OACurl: http://code.google.com/p/oacurl/
Mercurial: http://mercurial.selenic.com/
Maven: http://maven.apache.org/
HTML Tidy: http://www.w3.org/People/Raggett/tidy/
Blogger Dev Google group: http://groups.google.com/group/bloggerdev