Friday, October 13, 2006

Success! Posting to Blogger-beta Using Vim

If you see this, then I've finally (at least partially) figured out how to use the Blogger-beta GData API from Python. And to make posting a little bit quicker/easier for me, I've stuck that Python code into a Vim plugin.

To be honest, I'm not entirely sure what I was doing wrong in my previous attempts. I've changed several things, then changed some back. The resulting code really doesn't look much different to me than it did before, but there are a few very subtle changes. I suspect my ignorance when it comes to HTTP and XML protocols played a large part in my frustrations.

Anyways, here is a working version of the Python code that works for posting a blog entry. You will obviously need to fill in your BLOGID, GMAIL_ADDRESS, and GMAIL_PASSWORD appropriately for it to work for you:

#!/usr/bin/env python

import httplib2, re

account = "GMAIL_ADDRESS"
password = "GMAIL_PASSWORD"
blogid = "BLOGID"

def authenticate(h):
    auth_uri = ''
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    myrequest = "Email=%s&Passwd=%s&service=blogger&service=TestCompany-TestApp-0.0" % (account, password)
    response, content = h.request(auth_uri, 'POST', body=myrequest, headers=headers)
    if response['status'] == '200':
        return'Auth=(\S*)', content).group(1)
        return None

entry = """<?xml version="1.0" ?>
    <entry xmlns=''>
      <title type='text'>Test Post</title>
      <content type='xhtml'>
        <div xmlns="">
        If you are reading this, then it worked!

h = httplib2.Http()
uri = '' % blogid

# Get the Auth token from ClientLogin
auth = authenticate(h)
if auth:
    headers = {'Content-Type': 'application/atom+xml', 'Authorization': 'GoogleLogin auth=%s' % auth.strip()}
    response, content = h.request(uri, 'POST', body=entry, headers=headers)

    # blindly follow redirects
    while response['status'] == '302':
        response, content = h.request(response['location'], 'POST', body=entry, headers=headers)

    if response['status'] == '201':
        print "Entry successfully posted."
        print "Post failed: %s" % response['status']
    print "Authorization failed."

It's pretty rough, I realize, but I think it's good enough to get anyone started if you are interested. I hope someone will find this useful.

Now to see what other functionality I can perform :).

Oh, I almost forgot. If you want the Vim plugin I'm using you can get it here. To install it, drop it in ~/.vimrc/plugins/. To use it, open vim, type your post's subject on the first line, the post body below that, and type :BlogPost when you are done.


  1. props! you're well beyond "wannabe" now.

  2. And... how is possible to select tags for a post?

  3. hi sefra,

    Adding a label or tag to your post is actually quite easy. You simply add an extra XML element per tag to your entry that looks like this:

    <category scheme="" term="mycategory"/>

    This element will add the tag 'mycategory' to the post. As mentioned above, you would add one such element for each added tag.


  4. Thank you very much Dannis!!

  5. Hay man! This helps a lot... I am planning on building a small Python App about posting to Blogger and WordPress and this just helps a LOT! Thanks again!