Version 8 of Posting to Google's Blogger

Updated 2008-06-27 14:27:06 by dzach

dzach 2008-6-25: How can one post a message to a google Blogger blog programmatically using TCL?

Given the popularity of Google's Blogger platform, it might be a good idea to provide some basic functionality here. Google's Account Authentication API documentation can be found at:

 http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#ClientLogin

and a guide for the API here:

http://code.google.com/apis/blogger/developers_guide_protocol.html#CreatingPublicEntries

Here is the code I've tried so far (corrected with the help of PT in the Tcler's Chat):

 namespace eval ::google {

proc init {} {

        # packages http and tls are needed
        if {[catch {
                package require tls
                package req http
                } err]
        } {
                return $err
        }
        variable var
        variable login

        array set var {
                loginurl https://www.google.com/accounts/ClientLogin
                posturl http://www.blogger.com/feeds/<your-blog's-id>/posts/default
                timer_http 30000
        }

        array unset login
        array set login {
                accountType GOOGLE  
                Email <your_google_account_email>
                Passwd <your_google_account_password> 
                service blogger
                source <your_company-service-version>
        }
        ::http::register https 443 ::tls::socket
        set query [eval ::http::formatQuery [array get login]]

        # by specifying the -query option http::geturl sends a POST instead of a GET request, with a Content-type: application/x-www-form-urlencoded header
        # sent the POST request
        set token [::http::geturl $var(loginurl) -timeout $var(timer_http) -query $query]

        # to see what the reply was, uncomment the next line
        #parray $token

        upvar $token state
        # store login ids for later use
        foreach line [split [http::data $token] \n] { array set login [split $line =] }
        set login(httpcode) [lindex $state(http) 1]
        ::http::cleanup $token
        return $login(httpcode)
 }
 }; # end namespace ::gpost

A reply like:

 HTTP/1.0 200 OK
 Server: GFE/1.3
 Content-Type: text/plain 

 SID=DQAAAGgA...7Zg8CTN
 LSID=DQAAAGsA...lk8BBbG
 Auth=DQAAAGgA...dk3fA5N

should be returned by the server, where Auth=DQAAAGgA...dk3fA5N is the Auth (authorization) key to use in all subsequent requests (to be continued ...).