The experiences of a software developer as he wades through the dynamic world of technology. Discussions of new industry developments and current technologies he finds himself wrapped up in.

Thursday, August 17, 2006

Using Google's Blogger Data API

Google's Blogger Data API allows you to write client applications to manipulate Blogger content using GData feeds. For those not familiar with GData (Google Data API), Google defines it as follows:


A simple standard protocol for reading and writing data on the web. GData combines common XML-based syndication formats (Atom and RSS) with a feed-publishing system based on the Atom publishing protocol, plus some extensions for handling queries.


Google provides client libraries in both Java and C#, allowing for easy retrievals, additions, and updates of Blogger feeds. You can also manipulate the feeds manually, providing XML representing an entry. Unless you already have a client that constructs this data, I can see no reason not to use the client library Google provides - it really makes working with the feeds easy.

One thing you'll have to keep in mind is that Blogger is currently going through a transition phase. As it is now, Blogger uses it's own credentials, but there is a new version which is still in beta, that uses Google accounts. This can pose problems during any authentication phases (such as an update to an entry of a feed). The API docs do a good job of explaining how to handle this issue, so be sure to take note of it.

Like I stated earlier, using the Google Blogger API is easy. There is a general flow your application will have to follow to communicate with Blogger.

  1. Retrieve the feed URL for a blog.

  2. Using that feed URL, make a request to retrieve the blog's feed.

  3. Manipulate the feed, that is, add, update, or query a post.


Retrieving the feeds URL can be done by making an HTTP GET request to the blog's URL, that is, the one you would use in your web browser. Once you receive the response, you will have to parse the XML for the <HEAD> section. Here you will find the <link rel="alternate"> tags. Depending on which version of Blogger you're speaking to (current, or the new beta), there will be different feed URLs. Regardless of this, you should always use the Atom 1.0 version - may as well stay current.

Once you have the feed URL, you can use the Blogger API to get the actual feed. As Google explains in their documentation, you can write the following Java code to do this. Obviously you'll have to replace the URL string with the one you retrieved in the previous step. The credentials too will have match the Blogger account you are referring to.

URL feedUrl = new URL("http://www.blogger.com/feeds/blogID/posts/full");
GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1");

// Set up authentication (optional for beta Blogger, required for current Blogger):
myService.setUserCredentials("liz@gmail.com", "mypassword");

// Send the request and receive the response:
Feed myFeed = myService.getFeed(feedUrl, Feed.class);

// Print the title of the returned feed:
System.out.println(myFeed.getTitle().getPlainText());

To add a post to the blog via a web client, using the client library, you can use the following Java code as a guideline, as Google has provided (once again replacing the URL with the appropriate value).

googleService.setAuthSubToken(sessionToken, null);

URL postUrl = new URL("http://www.blogger.com/feeds/blogID/posts/full");
Entry myEntry = new Entry();

myEntry.setTitle(new PlainTextConstruct("Marriage!"));
myEntry.setContent(new PlainTextConstruct(
"<p>Mr. Darcy has <em>proposed marriage</em> to me!</p>
<p>He is the last man on earth I would ever desire to marry.</p>
<p>Whatever shall I do?</p>"));

Person author = new Person("Elizabeth Bennet", null, "liz@gmail.com");
myEntry.getAuthors().add(author);

GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1");

// Send the request and receive the response:
Entry insertedEntry = myService.insert(postUrl, myEntry);

The GData API also allows you to make queries against a feed based on a set of criteria you provide. This can range from searching for feeds with a specific instance of a word, or posts that fall between two dates. As Google has illustrated, the following code returns a feed containing the blog posts that fall between a specified date range.

URL feedUrl = new URL("http://www.blogger.com/feeds/blogID/posts/full");

Query myQuery = new Query(feedUrl);
myQuery.setUpdatedMin(DateTime.parseDateTime("2006-03-16T00:00:00"));
myQuery.setUpdatedMax(DateTime.parseDateTime("2006-03-24T23:59:59"));

GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1");

// Send the request and receive the response:
Feed resultFeed = myService.query(myQuery, Feed.class);

From the examples I have provided, I hope to have demonstrated how simple it can be to communicate with the Blogger service using the Google Blogger and GData APIs. For those who wish to integrate a blogging solution into their applications, these APIs provide a backbone to build upon. If you are serious about diving into these libraries, I highly suggest reading the full docs, as this post was simply to act as a primer, and get you excited. Keep in mind that Google also provides an API for Google Calendar. I've hacked around with it as well, and using it is very similar to the Blogger API. A GoogleBase API is also on the way, and I have the privelege of being a 'trusted tester', giving me early access. As the suite of Google APIs continue to grow, developers are getting the opportunity to push out some really kickin' apps.

Labels: , ,

0 Comments:

Post a Comment

<< Home