Archive for January, 2011

Posting to Blogger using API

As I wrote in previous post Blogger API has pretty low rate limit – 50. So if your case is thousands of posts use WordPress with XML-RPC, but if 50 is just suitable then code below can help.  Don’t forget to download API and dependencies

 

public class BloggerAutoPost {

 

     static final String EMAILACCOUNT = "googleaccount_email";

     static final String PASSWORD = "password";

     GoogleService service;

     String  blogId;

     Collection<Post> posts;

 

 

    public static void main(String[] args) throws Exception {

       BloggerAutoPost bloggerAutoPost = new BloggerAutoPost();

       bloggerAutoPost.load();

       bloggerAutoPost.login();

       bloggerAutoPost.getBlog();

       bloggerAutoPost.metaPost();

   }

 

    void load(){

      //initialize collection of posts  

 

    }

 

    void metaPost(){

        for (Post post : posts){

            post(post);

        }

    }

 

    private void login() throws AuthenticationException {

        service = new GoogleService("blogger", "test");

        service.setUserCredentials(EMAILACCOUNT, PASSWORD);

 

    }

 

    private void post(Post post) throws Exception {

        Entry myEntry = new Entry();

        myEntry.setTitle(new PlainTextConstruct(post.title));

        myEntry.setContent(new PlainTextConstruct(post.body));

        Set categories = myEntry.getCategories();

        Category category = new Category();

        category.setTerm(post.category);

        category.setLabel(null);

        category.setScheme("http://www.blogger.com/atom/ns#");

        category.setLabelLang(null);

        categories.add(category);

 

        // Ask the service to insert the new entry

        URL postUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");

        service.insert(postUrl, myEntry);

 

    }

 

    private void getBlog() throws MalformedURLException, IOException, ServiceException {

        // Request the feed

        final URL feedUrl = new URL("http://www.blogger.com/feeds/default/blogs");

        Feed resultFeed = service.getFeed(feedUrl, Feed.class);

 

        // Print the results

        System.out.println(resultFeed.getTitle().getPlainText());

        for (int i = 0; i < resultFeed.getEntries().size(); i++) {

            Entry entry = resultFeed.getEntries().get(i);

            blogId = entry.getId().substring(entry.getId().indexOf("-", 30) + 1);

            System.out.println("\t" + entry.getTitle().getPlainText());

            System.out.println(blogId);

        }

 

    }

 

}

 

class Post {

 

    String title;

    String body;

    String category;

 

    public Post(String title, String body, String category) {

        this.title = title;

        this.body = body;

        this.category = category;

    }

}

Blogger API post limits

 

I tried to post to Blogger  through API. In the next post I will provide code details. But when i finally polished my client and run it to post several thousands of posts,  I discovered that Blogger API have pretty low post limit. Only 50 posts can be post, after this number I got:

 

com.google.gdata.util.InvalidEntryException: Bad Request
Blog has exceeded rate limit or otherwise requires word verification for new posts

Workaround can be: wait 1 hour, then relogin and continue, but I simply bored from Blogger already. I come back to WordPress :) . WordPress XML-RPC client not more complex then Blogger and I already successfully posted almost 5000 posts per night.:)