Version 5 of Posting to Google's Blogger

Updated 2008-06-25 18:20:53 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 account
        variable var

        array set var {
                url https://www.google.com/accounts/ClientLogin
                timer_http 30000
        }
        array set account {
                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 account]]

        # 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(url) -timeout $var(timer_http) -query $query]

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

        if {[::http::status $token] ne "ok"} {
                # somethng went wrong
                error "Could not get a reply from $var(url) . [::http::status $token]"
        }
        set data [::http::data $token]
        ::http::cleanup $token
        return $data
 }

 }; # end namespace ::google

The above should return something like:

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

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

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