How To Use Java With Blogger: A Tutorial

Blogger has a REST API. I use it to retrieve particular posts or to post transformed data. There is no Java API that I know of, but you will see here it is not very difficult to interface with Blogger API in Java using plain old XML.

Using libraries commons-httpclient and DOM4J it would be quite easy to implement your own Java Blogger API as the following code will suggest.

All requests need to be authenticated and are done in HTTPS. I use common-httpclient to perform requests. Here is how to setup the client:

private  HttpClient initHttpClient()
HttpClient client = new HttpClient();
List authPrefs = new ArrayList(2);
authPrefs.add(AuthPolicy.DIGEST );
client.getParams().setParameter (AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);
Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
client.getState().setCredentials(new AuthScope( "", 443, AuthScope.ANY_REALM), defaultcreds);
return client;

Get Your Posts
To retrieve the posts, you just have to query the right url, and parse the XML response. I prefer to use DOM4J, because of its handy asXML() method to print a node as XML. For simplicity I use a Map to store an XML entry.

public Collection getPosts() throws  HttpException, IOException, ParserConfigurationException, SAXException, DocumentException 
GetMethod get = new GetMethod("" +"/"+blogId);
int statusCode = client.executeMethod(get);
if (statusCode != HttpStatus.SC_OK)
throw new RuntimeException(" Could not make HTTP request properly: " +get.getStatusLine());
InputStream response = get.getResponseBodyAsStream();
SAXReader reader = new SAXReader();
Document doc =;
Collection posts = new ArrayList();
List entries = doc.getRootElement().elements("entry");
if (LOG.isDebugEnabled())
LOG.debug("found "+entries.size()+" entries");
for (int i = 0; i <entries.size();i ++)
Element entry = (Element) entries.get(i);
Map m = new HashMap();
for (Iterator it = entry.elementIterator();it. hasNext();)
Element detail = (Element);
String name = detail.getName();
if (name.equals("link"))
m.put("link ",detail.attribute("href").getValue());
else if (name. equals("content"))
if (LOG.isDebugEnabled())
LOG.debug( "found="+m.get(" title")+", url= "+m.get("link"));
return posts;

Create XML for a new Post
Nothing particular here, just XML production.
private String createXmlForCreatePost(String postTitle, String postContent) throws   IOException, DocumentException
SAXReader xmlReader = new SAXReader();
xmlReader.setValidation(false );
Document doc = DocumentHelper.createDocument();
QName rootName = DocumentHelper.createQName("entry", new Namespace("", " "));
Element root = doc.addElement(rootName);
Element title = root.addElement("title");
title.addAttribute("mode"," escaped");
title.addAttribute("type ","text/plain");
title.setText (postTitle);
Element generator = root.addElement("generator ");
generator.addAttribute("url" , "");
generator. setText("31416 Java Generator ");
Element content = root.addElement("content ");
content.addAttribute("type" , "application/xhtml+xml");
//Element div = content.addElement(DocumentHelper.createQName("div",new Namespace("","")));
//div.add(…); //YOUR XHTML HERE
StringWriter result = new StringWriter();
XMLWriter writer = new XMLWriter (result);
return result.toString();

That’s it

comments powered by Disqus
Tweet Submit to reddit
© 2006-16 Fabien Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License.