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.

7 comments:

  1. This has recently stopped working. Looks like Google have introduced a new http get parameter, "tok", that you have to pass when requesting the export. Its a large string id and I'm not sure how to get it, anyone find out, let me know.

    If I have no luck at somepoint I'll have to write a Java app to export these.

    Regards
    Parwy

    ReplyDelete
  2. okay figured this out, added the following

    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`

    ReplyDelete
  3. This solution nolonger works. Looks like google have modified their authentication mechanism.

    ReplyDelete
  4. Yes - does anyone have any workarounds yet?

    ReplyDelete
  5. too bad this doesnt work anymore, im also looking for a possibility to export contacts via a bashscript. actually from what i found on google: https://developers.google.com/google-apps/contacts/v3/

    ReplyDelete
  6. if still of interest, ive come up with this script to grab google contacts:

    # reference:
    # https://developers.google.com/gdata/articles/using_cURL
    # https://developers.google.com/gdata/faq#clientlogin
    # https://developers.google.com/google-apps/contacts/v1/developers_guide_js

    Email="your@mail.acc"
    Passwd="Y0urP455w0rd"
    Format="json" # default: xml/atomfeed

    # login
    Auth=$(curl --silent https://www.google.com/accounts/ClientLogin --data-urlencode Email=$Email --data-urlencode Passwd=$Passwd -d accountType=GOOGLE -d source=Google-c$
    # get contacts
    contacts=$(curl --silent --header "Authorization: GoogleLogin auth=${Auth#*=}" "https://www.google.com/m8/feeds/contacts/default/full?alt=$Format")

    a nice json parser for shell scripts can be found here: https://github.com/dominictarr/JSON.sh#jsonsh

    ReplyDelete
  7. Thanks would really really be interested if you manage to get the download in full VCARD format with all the data... as if the download had occurred manually via the online export.

    ReplyDelete