Recent Book Reviews
Review: Sideshow – Dumbing Down Democracy
Review: Mud, Sweat, and Tears: Bear Grylls The Autobiography
Review: The Mar Saba Codex
Review: Guantanamo – My Journey
Review: Justification: God’s Plan and Paul’s Vision
See more

Copying Gmail Contacts between accounts

January 21st, 2010.

I want to copy my gmail contacts to my wife’s account. Ideally I would like to share them like I share my online calendar but Google provides no mechanism for this.  That is annoying.

So annoying, in fact, that it sparked a hulk-like welling up of my inner geek and the production of this python code to at least do the copying. It doesn’t sync or share, it just copies.

It’s a blunt instrument, but it does the job.

# -*- coding: utf-8 -*-

import atomimport gdata.contactsimport gdata.contacts.serviceimport gdata.data

gmailuser1="source gmail user"gmailpw1="source gmail password"gmailuser2="destination gmail user"gmailpw2="destination gmail password"

print "Connecting to gmail as "+gmailuser1;

gml1 = gdata.contacts.service.ContactsService()gml1.email = gmailuser1gml1.password = gmailpw1gml1.source = 'Ponstructor-GmailCtctCopy-1.0'gml1.ProgrammaticLogin()

print "Connecting to gmail as "+gmailuser2

gml2 = gdata.contacts.service.ContactsService()gml2.email = gmailuser2gml2.password = gmailpw2gml2.source = 'Ponstructor-GmailCtctCopy-1.0'gml2.ProgrammaticLogin()

print "Obtaining contacts feed from "+gmailuser1feedquery = gdata.contacts.service.ContactsQuery()feedquery.max_results = 9999gmlfeed = gml1.GetContactsFeed(feedquery.ToUri())

print "Adding contacts to "+gmailuser2for index,gmlctct in enumerate(gmlfeed.entry):        if gmlctct.title.text != None:                print "Adding "+gmlctct.title.text                setattr(gmlctct,'group_membership_info',None)                setattr(gmlctct,'id',None)                setattr(gmlctct,'link',None)                try:                        entry = gml2.CreateContact(gmlctct)                except gdata.service.RequestError,err:                        if err.args[0]["status"]==409:                                print "Conflict - that's fine keep moving"                        elif err.args[0]["status"]==400:                                print "Bad Request - can't do much about it"                        else:                                raise err

3 comments for “Copying Gmail Contacts between accounts”

  1. John Tongue says:

    Geek, indeed!

  2. Thomas says:

    Good work Will :) Could you not have simply used the export/import features instead? – I know it's a one-off copy and doesn't sync, but neither does your script…I use goosync.com to sync my google calendar with my (ancient) palmOS device. It also has features to sync contacts from gmail to devices, not sure it can sync between gmail accounts though.Google should allow you to share contact groups ala google docs sharing. One day perhaps…

  3. Will Briggs says:

    Hi Thomas, probably could of. I have a script that originally uploaded from my (custom – nothing I know of handles pastoral contacts properly) contacts database and this just forks off that. Consider it part of a larger whole :-) … which will include syncing at some point.

Leave a Reply

Identify yourself using a social network:

Connect with Facebook

Or provide details manually (*=required):

*

 

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>