13 July 2009

Export your contacts from your Google Apps account automatically as a vcard file

10/10/2009: edited as Google now requires a field called GALX to be posted too.
08/01/2011: edited as Google now requires a field called tok to be posted too.
16/05/2012: this solution nolonger works, looks like google have modified their authentication mechanism.

To export your contacts from your Google Apps account automatically as a vcard file:

assumptions:
google apps domain: acme.com
user: doej
password: mypassword

wget "https://www.google.com/a/acme.com/ServiceLogin" --no-check-certificate --save-cookies="/tmp/gcookie" --keep-session-cookies --output-document=/dev/null

GALX=`grep "GALX" /tmp/gcookie | cut -f 7`

wget "https://www.google.com/a/acme.com/LoginAction2" --post-data="service=contacts&Email=doej&Passwd=mypassword&GALX=${GALX}&PersistentCookie=yes&continue=http://www.google.com/contacts/a/acme.com" --no-check-certificate --load-cookies="/tmp/gcookie" --save-cookies="/tmp/gcookie" --keep-session-cookies --output-document=/dev/null

wget "https://www.google.com/contacts/a/c/acme.com/ui/ContactManager?titleBar=true&hl=en&dc=true#" --no-check-certificate --load-cookies="/tmp/gcookie" --save-cookies="/tmp/gcookie" --keep-session-cookies --output-document=/tmp/gtoken

TOKEN=`grep "var tok" /tmp/gtoken | cut -c 16-72`

wget "http://www.google.com/contacts/a/c/acme.com/data/export?exportType=GROUP&groupToExport=%5EMine&out=VCARD" --no-check-certificate --load-cookies="/tmp/gcookie" --output-document="/tmp/addressbook.vcf"


Note: In the first wget, by continuing to mail.google.com and specifying keep-session-cookies, the cookie file, /tmp/gcookie, will contain entries for the mail.google.com. This is required for the second wget.