Interacting with Atom at Blogger
The first hurdle in working with any web-based API is always authentication. However, in the grand scheme of things, that’s easy here. Authentication is performed via straight HTTPS, and no special user accounts, permissions, or tokens are required to use an application that interacts with your blog. You simply need to have a valid Blogger account and be a member of one or more blogs.
The Blogger API is accessed via this URL: https://http://www.blogger.com/atom/. Accessing and logging into that URL will return an XML document describing the services available to you and the URLs that should be used to perform those functions, as shown in Listing 1.
Listing 1 Atom services XML.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> <feed xmlns="http://purl.org/atom/ns#"> <userid xmlns="http://www.blogger.com/atom/ns#">12345</userid> <link href="https://http://www.blogger.com/atom/67890" rel="service.post" title="Words from Tim" type="application/atom+xml"/> <link href="https://http://www.blogger.com/atom/67890" rel="service.feed" title="Words from Tim" type="application/atom+xml"/> </feed>
Two services are described in the example in Listing 1: service.post and service.feed. Each one is associated with a particular blog—"Words from Tim," in this example—and each has a particular associated URL. If your Blogger account is a member of multiple blogs, multiple sets of service.post and service.feed link elements would be returned, each giving you the correct URL to perform the specified functions for the specified blog.
To view and edit recent entries to your blog, you simply need to perform an HTTP GET on the URL provided in the link element in Listing 1. That will provide you with another XML document containing one entry element for each blog entry, like the one in Listing 2.
Listing 2 Retrieving posts.
<entry xmlns="http://purl.org/atom/ns#"> <link href="https://http://www.blogger.com/atom/67890/11223344" rel="service.edit" title="Test Post" type="application/atom+xml"/> <author> <name>Tim Stevens</name> </author> <issued>2005-08-25T17:52:00-04:00</issued> <modified>2005-08-25T21:52:31Z</modified> <created>2005-08-25T21:52:31Z</created> <link href="http://www.example.com/alternate.html" rel="alternate" title="Test Post" type="text/html"/> <id>tag:blogger.com,1999:blog-67890.post-11223344</id> <title mode="escaped" type="text/html">Title of an Example Post</title> <content type="application/xhtml+xml" xml:base=" http://www.example.com/" xml:space="preserve"> <div xmlns="http://www.w3.org/1999/xhtml">Content of an example post</div> </content> <draft xmlns="http://purl.org/atom-blog/ns#">false</draft> </entry>
The author, title, and content are all encoded in their respective elements, along with some other miscellaneous properties describing the entry, such as whether it’s a draft or an alternate URL. Most importantly for this example, however, is the link element at the top, which gives the URL to HTTP PUT an edited version to, using the service.edit function. Editing the specified blog entry is just a matter of PUTting a modified version of the entry element with updated titles or content or whatever to the specified URL. To delete an entry, send an HTTP DELETE to that URL.
Posting is similar, but instead you PUT a new XML entry to the initial URL provided in the service.post link element. For error handling, you really only need to worry about 401 errors, thrown when a user logs in incorrectly or attempts to do something he or she doesn’t have permissions to do, and of course 404 errors if a request is made to retrieve an entry that was deleted. However, an HTTP 500 (internal server) error may be thrown incorrectly at times, and so should be handled as well.
Posting, editing, deleting, and browsing are the primary functions represented by the Atom API. Although talking directly to Blogger’s API interface with HTTP and XML syntax isn’t terribly complicated, as always it’s better to reuse than to re-create, and a number of Atom-related projects are available to handle the dirty work.
Few libraries cover the entirety of what Atom potentially offers, but one simple library called Atomizer, for .NET applications, includes all the functionality required for interacting with your blog. We’ll be using Atomizer for the C# code examples presented here.