17 December 2008

First, How to Post to Any API that supports the (admittedly) outdated MetaWeblog API.

How to Publish A Blog Entry For Roller 4.0 Software I found very little in the way of a solid API in Java on top of this XMLRPC API. However, I did find quite a bit using just good old Apache XMLRPC 3.0. Here's how that solution looks:


 /**
 * @param serverUrl if youve unpacked a stock Roller 4.0 url, it's
likely going to be http://www.YOURHOST.com/roller-services/xmlrpc
 * @param user the user you have configured on Roller itself
 * @param the password for the user name
 * @param blogId the id of the blog. When you log into the Admin
screen most pages will mention it.
 * @param title the title of the post
 * @param content the content of the post (can be in HTML)
*/
  public String createPost( String serverUrl, String user, String
pass, String blogID, String title, String content) throws IOException
{
        String ret = null;
        try {
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL(serverUrl));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Map m = new HashMap();
            m.put("title", title);
            m.put("description", content);
            m.put("dateCreated", new Date());
            m.put("category", "Java");
            Object[] params = new Object[]{blogID, user, pass, m, true};
            ret = (String) client.execute("metaWeblog.newPost", params);
        } catch (Throwable th) {
            System.out.println( "blam!") ;
        }
        return ret;   // a unique ID generated by Roller
    }

How to do the same thing with Blogger Usign Google's GData.


 GoogleService service = new GoogleService("blogger", "");
        service.setUserCredentials(user, pass);
        Entry myEntry = new Entry();
        myEntry.setTitle(new PlainTextConstruct(title));
        myEntry.setContent(new PlainTextConstruct(content));
        Person author = new
Person(StringUtils.defaultString(authorName), null, null);
        if (!StringUtils.isEmpty(authorName)) myEntry.getAuthors().add(author);

        URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID
+ "/posts/default");
        return service.insert(postUrl, myEntry);

The Google Data stuff was using the Java Google Data APIs. The XMLRPC library is quite formidable. I'd not recommend using Apache XMLRPC 3.1.1 from Maven, as it didn't seem to be a valid artifact.