Jun 23, 2010

f Comment

How to Add Labels To A Post Via Google Blogger Java API?

The Issue
Amazon Many bloggers using Google Blogger Java API are wondering how to set the labels of a post via their API, and I found a solution after much trial and error For those who don't know a label is a field of a post where you can put in a comma-delimited list of words that describe this post. In the Blogger admin page it looks like this:

Google Blogger Create A Post Admin Page Labels Field

For example this post you are reading right now is about how to create labels for a post through Blogger's Java API, and it can have "Blogger,Java" as the labels. Doing so in the admin page is easy, but what about doing so programmatically via Blogger's Java API?

Solution
Follow the following steps.

Step 1: Import Necessary Java Blogger API Libraries
They include but not limited to the following:
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Category;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.HtmlTextConstruct;
import java.net.URL;
Step 2: Create an Entry Instance
Use Blogger API to create an entry that simulates an actual blog post.
Entry myEntry = new Entry();
myEntry.setTitle(new PlainTextConstruct("some title"));
myEntry.setContent(new HtmlTextConstruct("some HTML body"));
// set whatever further attributes you want...
Step 3: Get Reference to Categories of the Entry Instance
Set categories=myEntry.getCategories();
Step 4: Create a Category
Create a Category as follows. Note you need to set label and labelLang to null. And you MUST set scheme properly. I would think if I didn't setLabel and setLabelLang they'd be set to null by default, but it turns out that's not true.
Category category= new Category();
category.setTerm("whatever label you like such as Google Blogger or Java API");
category.setLabel(null);
category.setScheme("http://www.blogger.com/atom/ns#");
category.setLabelLang(null);
Step 5: Add the Category to the Categories Reference
Use the following code to add the category we just created to the reference to categories.
categories.add(category);
To create more categories simply repeat the step above and this step. Do NOT concatenate them with commas and set it via setTerm(). For example suppose you want to add "Java API,Google Blogger" as the labels. You'd create a category for "Java API" (let's call it categoryJavaApi) and a category for "Google Blogger" (let's call it categoryGoogleBlogger) and add them to the categories reference like the following:
categories.add(categoryJavaApi);
categories.add(categoryGoogleBlogger);
Any questions?

Step 6: Create this post on Your Blog via API
// suppose your blog ID is 123456
URL postUrl = new URL("http://www.blogger.com/feeds/123456/posts/default");
myService.insert(postUrl, myEntry);
That's it! If you encounter any difficulty don't hesitate to let me know!

By the way some people are wondering how to retrieve all categories of a blog, with duplicates removed. Here's how you do it.

How to Retrieve All Categories of a Blog via Java API
public static void printAllPosts(GoogleService myService, String blogId)
  throws ServiceException, IOException {
  // Standard way to request the blog given blog ID
  URL feedUrl = new URL("http://www.blogger.com/feeds/" 
    + blogID + "/posts/default");
  Feed resultFeed = myService.getFeed(feedUrl, Feed.class);

  // Loop through and put every post's categories in a HashSet
  Set categories=new HashSet();
  for (int i = 0; i < resultFeed.getEntries().size(); i++) {
  Entry entry = resultFeed.getEntries().get(i);
  categories.addAll(entry.getCategories());
  }

  // Now variable 'categories' contains all unique categories!
}
Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael