Thursday, February 04, 2010

Using the XMLRPC interface of Confluence in Python

I was successful in creating a new page and in posting to the blog in my personal space thanks to some info here. Python comes with everything needed to use the interface. An example script consists of these parts:
from xmlrpclib import Server  s = Server("https://server/rpc/xmlrpc") token = s.confluence1.login("user", "password") 
Now you can use many of the API functions. For instance you can create a page:
newpagedata = {"title":"Python example New Page 2","content":content,"space":"spacekey"} newpage = s.confluence1.storePage(token, newpagedata) 
where "newpagedata" is a dictionary that has at a minimum the keys "title", "content", and "space". You can also update an existing page by getting a reference of it and replacing the content.
page = s.confluence1.getPage(token, "spacekey", "Python example New Page") page["content"] = "Updated text" page = s.confluence1.storePage(token, page) 
The same process works basically the same way for blog posts, except the functions are "storeBlogEntry" and "getBlogEntry" if you know the ID, or "getBlogEntryByDayAndTitle" if you know that.
Many more functions are available at the link above.