Manage blog data via the Blogger Data API

Developer Tony Patton shows how the Blogger Data API makes it easy to manage blog content via code. You can create a new entry, query blogs, and much more with the API.

While the popularity of social media has skyrocketed, blogs continue to be useful to push information to a wide audience. Some may consider a blog to be social media, but the definition on Wikipedia seems to say otherwise, but it is debatable given how a blog is utilized. With that said, a blog is a great vehicle for pushing information to a wide audience on any subject ranging from Phillies baseball to cloud computing. Blogger is one of the more popular blogging platforms available, and the Blogger Data API makes it easy to manage blog content via code.


The Blogger Data API provides access to blog data via a Google Data API feed. That is, the Google Data API is a standard vehicle for working with Google-based applications/data such as Google Calendar, Google Docs, and so forth. You can do everything you need with blog posts via the API. The possibilities are endless, as you can pull blog data to display on a Web site or an application, create blog entries from other applications, and so forth.

Using the API

The Blogger Data API can be used by utilizing the client library files within your code. For this post, the .NET client library is used. The download includes all necessary DLL files along with documentation and samples. The examples in this post utilize the client and blogger DLLs, which includes these files:

  • Google.GData.Blogger.dll — includes all functionality for working with Blogger.
  • Google.GData.Client.dll — base functionality for working with Google.

With the client library installed, you are ready to put it to use as long as you have a Google account.

Accessing Google

The first step to interacting with Google is authentication. With Blogger, it is as simple as your Google username and password. This is the same set of credentials used to access Gmail. The first step is passing your login credentials to the Service constructor. The first parameter of the Service is the name of the service that will be used, and the second parameter is a name assigned to the application. The next step creates an instance of the RequestFactory and then set its account type to Google. The following code snippet shows how this can be accomplished via C#:

Service bService = new Service("blogger", "TRExample");

bService.Credentials = new GDataCredentials("", "pw");

GDataGAuthRequestFactory bRequest = (GDataGAuthRequestFactory)bService.RequestFactory;

bRequest.AccountType = "GOOGLE";

Creating a new entry

With authentication complete, you are ready to work with blog posts. It is a straightforward process using the AtomEntry class. Entry details are set using the Insert method of the Service class, which accepts the Blogger address as the first parameter and the new post as the second. The following code posts a new blog entry:

try {

Service bloggerService = new Service("blogger", "TRExample");

bloggerService.Credentials = new GDataCredentials("", "pw");

GDataGAuthRequestFactory bloggerRequest = (GDataGAuthRequestFactory)bloggerService.RequestFactory;

bloggerRequest.AccountType = "GOOGLE";

AtomEntry newPost = new AtomEntry();

newPost.Title.Text = "TechRepublic test post";

newPost.Content = new AtomContent();

newPost.Content.Content = "<div xmlns=''>" +

"<p>This is a test post!!</p></div>";

newPost.Content.Type = "xhtml";

newPost.Updated = new DateTime(2011, 1, 1, 10, 0, 0);

newPost.Published = new DateTime(2011, 1, 1, 10, 0, 0);

Uri blogFeedUri = new Uri("");

AtomEntry createdEntry = bloggerService.Insert(blogFeedUri, newPost);

} catch (System.Exception ex) {

Console.Writeline("Exception: " + ex.InnerException.ToString());

}  }

Blog queries

Reading blog entries is as simple as creating a FeedQuery object, passing it to the Query method of the Service class, and looping through the results. The FeedQuery object has methods to define it. The MinPublication and MaxPublication properties define the start and end date for entries to be read, while the NumberToRetrieve specifies the number of items to return. The FeedFormat property allows you to specify the format that is useful if you want to utilize RSS, and the Categories property lets you specify entry categories to refine the search. The following code takes this a step further by pulling blog entries and writing them to local disk:

Service bService = new Service("blogger", "TRExample2");
bService.Credentials = new GDataCredentials("", "pw");
GDataGAuthRequestFactory bRequest = (GDataGAuthRequestFactory)bService.RequestFactory;
bRequest.AccountType = "GOOGLE";
FeedQuery query = new FeedQuery();
query.Uri = new Uri("");
query.MinPublication = new DateTime(2003, 1, 1);
query.MaxPublication = new DateTime(2010, 12, 30);
query.NumberToRetrieve = 2500;
AtomFeed feed = bService.Query(query);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\bout.txt")) {
foreach (AtomEntry entry in feed.Entries) {
file.WriteLine(entry.Updated.ToShortDateString() + " " + entry.Updated.ToShortTimeString());
file.WriteLine("Entry Title: " + entry.Title.Text);
} }

The result is a local file containing the blog entry's post date and time, title, and the body of the entry. An alternative way to export a blog is using a HTTP GET command, where you will need the ID of the blog to export:


The following code snippet demonstrates sending such a request and accessing the results via a Stream object:

StringBuilder sb = new StringBuilder();

byte[] buf = new byte[8192];

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("");

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Stream rStream = resp.GetResponseStream();

Another option with reading blog data is working with the comments. The following URL format provides access to all comments for a blog, and it can be used as the URI for a FeedQuery object to access blog comments:

The issue with working with comments is extracting the blogID from the blog address since there is no property that makes it readily available. The SelfUri property of a blog provides access to its address that contains the blogID. The blogID follows the last forward slash (/) in the value, so you can easily extract the blogid using String class methods or regular expressions. The following code sample loops through all blogs for a user account. For each blog it loops through all entries followed by all comments posted to the blog and displays content in the console.

Service bloggerService = new Service("blogger", "Comments");
bloggerService.Credentials = new GDataCredentials("", "pw");
GDataGAuthRequestFactory bloggerRequest = (GDataGAuthRequestFactory)bloggerService.RequestFactory;
bloggerRequest.AccountType = "GOOGLE";
FeedQuery bQuery = new FeedQuery();
bQuery.Uri = new Uri("");
AtomFeed bFeed = null;
bFeed = bloggerService.Query(bQuery);
foreach (AtomEntry entry2 in bFeed.Entries) {
FeedQuery query = new FeedQuery();
query.Uri = new Uri(entry.FeedUri.ToString());
query.MinPublication = new DateTime(2003, 1, 1);
query.MaxPublication = new DateTime(2011, 12, 30);
query.NumberToRetrieve = 25;
AtomFeed feed = bloggerService.Query(query);
foreach (AtomEntry iEntry in feed.Entries) {
Console.WriteLine("Entry Title: " + iEntry.Title.Text);
string blogURI = entry.SelfUri.ToString();
int slash = blogURI.LastIndexOf('/') + 1;
int end = blogURI.Length - 1;
string blogid = blogURI.Substring(slash, (end - slash + 1));
Uri commentPostUri = new Uri("" + blogid.ToString() + "/comments/default");
if (commentPostUri != null) {
FeedQuery commentQuery = new FeedQuery();
query.Uri = commentPostUri;
AtomFeed commentFeed = bloggerService.Query(query);
foreach (AtomEntry comment in commentFeed.Entries) {
Console.WriteLine("Comment: " + comment.Title.Text);
} } } }

Everything accessible via code

It amazes me to think back to the early days of the Web when you were basically starting from scratch with most tasks, so there was lots of code to write. Today, there are powerful applications with publicly available APIs to take advantage of the functionality in your own code. Google is a great example; the company offers its many applications and services available with the Blogger Data API, providing access to the Blogger service. It allows you to easily work with Blogger entries without accessing the site.

With so many Google services available, which ones are you using or plan to use in the future? What problems have you encountered with these APIs? Share your thoughts in the discussion.

By Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...