On syncing with Google Contacts

    So, I started with a new company a few weeks ago, and one of the things I missed from my previous company was having the entire corporate directory synced onto my phone. Its really handy as an on caller to be able to give people a call when something goes wrong, without having to dig around and find their details.

    Back in the good old days at Google the way you got this sort of data onto your phone was to run a script written by one of the guys on the gmail team. The script grabbed the LDAP directory, and pushed it into Google contacts, which you could then sync with your phone. Now I wanted something very similar -- especially as the contacts sync stuff with Android is pretty reasonable.

    However, I'd never coded with the Google public APIs before, and that turned out to be the hardest part of the problem.

    First off I wrote a little script which dumped the corporate directory into a text file. I mostly did this because I wanted other people to be able to run the script in as light weight a manner as possible -- for example, if we wanted to roll this out for hundreds of people, then you wouldn't want to run the LDAP query hundreds of times. The format for my text file is kinda lame to be honest:

      Michael Still: {'telephoneNumber': ['+61 123 123 123'], 'ID': ['mikalstill'], 'mail': ['mikal@stillhq.com']}
      


    So, you get the user's name, then a python dictionary with three keys in it. There isn't any particular reason for having just three keys, it was just the three fields I thought were most interesting at the time. Note that each field is an array. A simple human readable format like this means that I can also grep through the file if I ever quickly want a user's details, which is a nice side effect.

    The most important thing I learnt here is that the ID field is really important. If you don't have something you feel you can use there, then you might need to synthesize something -- perhaps an ascii representation of the user's name or something. This is important because I discovered that Google rewrites Unicode characters you ask it to store, so if you do a simple text comparison against the user's name, then you might get a false negative and end up creating more than one entry for that user. That was particularly a problem for me because there are a fair few people in the company with European accented characters in their names.

    The docs for the Google contacts API are ok, although I did have to spend some time randomly searching for examples of some of the things I wanted to do. For example, the docs didn't have an example of how to store a phone number that I could find. Also, I am a little shocked to discover there is no query interface in contacts for contact name. This seems like a pretty massive oversight to me, but here's what the docs have to say on the issue:

    For more information about query parameters, see the Contacts Data API Reference Guide and the Google Data APIs Reference Guide. In particular, there is no support for full-text queries or locating a contact by email address.


    Whatever intern wrote the API should have his ball pit rights revoked until he fixes that. After that it was all gravy. Here's the code: http://www.stillhq.com/svn/trunk/google-contacts/pushdirectory.py.

    I note that there is an enterprise shared contacts API (see here), but you have to be a premiere customer for it to work.

    Tags for this post: google ldap directory sync contacts android python
    Related posts: Offline Thunderbird; Cool people I have met at work; The android and I; Implementing SCP with paramiko; Packet capture in python; mbot: new hotness in Google Talk bots

posted at: 02:32 | path: /google | permanent link to this entry