Uncategorized

How to add a new Blogger post with labels from C#

First download and install Google API from here:
https://code.google.com/p/google-gdata/downloads/list

Add a reference to “Google Data API Core Library”,
and use the following function:

public static bool AddPost(string title, string bodyHTML, string[] labels)
{
    Service service = new Service(“blogger”, “<program name>”);
    service.Credentials = new GDataCredentials(“<username>”, “<password>”);
    AtomEntry newPost = new AtomEntry();
    newPost.Title.Text = title;
    newPost.Content = new AtomContent();
    newPost.Content.Content = bodyHTML;
    newPost.Content.Type = “html”;
    foreach (string label in labels)
    {
        AtomCategory cat = new AtomCategory();
        cat.Scheme = new Uri(“https://www.blogger.com/atom/ns#”);
        cat.Term = label;
        newPost.Categories.Add(cat);
    }
    AtomEntry response = null;
    try
    {
        response = service.Insert(new Uri(“<uri>”), newPost);
    }
    catch (GDataRequestException exception)
    {
        if (exception.ResponseString == “Blog has exceeded rate limit or otherwise requires word verification for new posts”)
        {
            return false;
        }
        else
        {
            throw exception;
        }
    }
    if (response == null)
    {
        throw new Exception(“Something went wrong”);
    }
    return true;
}

Note what you need to replace:

  • <program name>
  • <username>
  • <password>
  • <uri>

Also note the marked yellow line. It took me some nasty googling time to find it. You must put it for the labels to work.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.