If you had ever been curious about how to code a Google Internet Gadget, you most likely had put your hands on programming tools such as Google Gadget Editor for iGoogle.

I had coded a few gadgets so far. Although I didn’t spent much time with the editor, there were a couple of, let’s say features, I disliked getting the feeling that they were slowering down my work letting me be, in turn, less productive. Things like:

  • Automatic refreshing, opened helloworld.xml sample to start coding my gadget, and stayed idle for a couple of minutes you get helloworld gadget refreshed throwing all your effort till then right to the dustbin.
  • Cursor is set to first position of the text editor box after saving a file, super-inconvenient.
  • On my Lenovo T61 laptop, pressing the key which is just left to the upper-arrow key, made the gadget to reload without confirmation. I know, this is more related to my own my settings, but cause me much headache (since it’s easy to rubber the key by chance when moving upwards)
  • All the inconvenience of using any other text-editor when you are a Vim lover.

All this led me to code a command-line Perl script to upload a Google Internet Gadget to iGoogle.

There is not such a thing as an API for iGoogle to help programmers to interact with iGoogle, so all work has to be coded in an old-school fashion, pretending your script is in fact a browser and has to communicate with the iGoogle server in the same manner.

If you are a Perl programmer, you most likely be familiar with popular packages such as LWP (lib www Perl, although this acronym always reminds me to Larry Wall’s Perl). Client-Server HTTP communication defines a client as an user-agent, and that is all what LWP is about. First, instance a class called UserAgent, think of it as a XMLHttpRequest object is you are familiar with Javascript, then codify your http request, send and wait for a response.

Before stating coding, I used Wireshark, formerly known as Ethereal, to peer at how Firefox should interact with iGoogle. After that, the goal is to gathered all needed information iGoogle would expect from a browser request. The most difficult part here was to figure out were to get the SID cookie from.

Many of the cookies iGoogle expect from an HTTP request can be retrieved from your cookies.txt file (or querying moz_cookies table if you are using Firefox 3). All values stored in cookies.txt are permanent cookies. The SID cookie is in fact a session cookie, and by hence, should be stored in sessionstore.js file. You can also retrieve this value by successfully authenticating against Google using ClientLogin mechanism.

Summarizing, if your browser has an already opened Google Session, our script should peer at sessionstore.js file to fetch SID from it. If there is not a Google session opened within your browser, then our script should authenticate against iGoogle to get that specific value. Google ClientLogin mechanism is aimed just to retrieve a valid Auth token, necessary to interact with all Google’s services and APIs. The docs even claim that “SID and LSID are not currenly activated ans should not be used“, but here you see how this SID token can be used to successfully interact with Google’s services (upload files to iGoogle, Google Page Creator, etc) which lack an API.

Script to upload Google Internet Gadgets to iGoogle: download.
You need to set the following variables:

  • username, you Google user name (without @gmail.com)
  • password, your Google account password

NOTE: If a Google session is opened within your Firefox browser (that means, you are already logged in at least one Google service, when you use this script), then you can leave this two values empty.

  • GG_ID (Google Gadget ID), every iGoogle user has a Google Gadget ID. To know your GG_ID, you must figure out what your target URL looks like. When the Save… option is pressed, Google Gadget Editor sent a POST request to a target URL whick looks like:

http://www.google.com/ig/gadgets/file/$GG_ID/$filename?synd=gde&et=itJbVyUh

Where:

  • filename, is the name of file to upload
  • GG_ID, is your Google Gadget ID.

Use Wireshark or Firebug to figure out what is the value for your GG_ID. Please, let me know if there is a better way to get this value.

Perl libraries required:

  • POSIX
  • DBI
  • Data::Dumper
  • Config::General
  • LWP

NOTE: I tested this script on Firefox 3 (remember is needed to fetch cookies from whatever cookie store mechanism, sqlite3 in case of Firefox3). I added code to read cookies from cookies.txt (cookie store mechanism in Firefox 1 & 2), but haven’t tried it.