10

GMail has a handy-dandy window that lets you export all of your contacts as a CSV file. If you do this then look at your browser's history, you see that the URL used to initiate the download looks something like:

https://mail.google.com/mail/c/data/export?exportType=GROUP&groupToExport=%5EMine&out=GMAIL_CSV&tok=rQt5BTUBABA.N0gr2-zKkG9868fM7tF_FQ.fOgeVayblkGavK2AuFjZ2t

I would like to write a batch script in Python that will automatically download this CSV file every night to my server, but I cannot figure out how to get the "tok". I've looked into Google's OAuth, but it seems to be a human-interactive process, whereas I need this to happen at 3AM when all available humans will be asleep.

Can this be done, or has Google made my e-mail account so "secure" that I cannot access it via an unattended script?

Thank you! Bryon M. Elliott

2
  • You can always do it with selenium or something: programmatically open a browser window, sign into gmail, etc, etc.
    – Katriel
    Jan 19, 2012 at 15:21
  • Also I think Backupify can do this, so it's certainly possible.
    – Katriel
    Jan 19, 2012 at 15:25

4 Answers 4

5

First authenticate with your account and password (see http://developers.google.com/accounts/docs/AuthForInstalledApps)

auth=`curl --silent -d Email=whatever@gmail.com -d Passwd=yourpwd -d service=contacts https://www.google.com/accounts/ClientLogin|grep ^Auth=|cut -d= -f2`

Obtain the myserious "tok" variable. I found out that it's part of a JSON request:

tok=`curl --silent --header "Authorization: GoogleLogin auth=$auth" "https://www.google.com/s2/gastatus?out=js&rc=0" |sed 's/,/\n/g' |grep AuthToken |cut -d'"' -f6`

Download the CSV (in Google Format). This is exactly the same as the manual way of doing this: support.google.com/mail/answer/24911?hl=en

curl -s --stderr - -L -o contacts-google-whatever\@gmail.com-$(date +%Y-%m-%d-%H.%M.%S).csv -H "Authorization:GoogleLogin auth=$auth" --insecure "https://www.google.com/s2/data/exportquery?ac=false&cr=true&ct=true&df=true&ev=true&f=g2&gids=6&gp=true&hl=en-US&id=personal&max=-1&nge=true&out=google_csv&sf=display&sgids=6%2Cd%2Ce%2Cf%2C17&st=0&tok=$tok&type=4"

The variable "out" can be one of google_csv, outlook_csv, vcard. My code is bash, you might want to change the curl commands to a Python equivalent. The important information are the variables and URLs.

1
2

i was looking for some similar solution and cause i couldnt find any ready solution ive done it myself: (pls bear with me for the lazy scripting, just wanted to get it working :D)

#!/bin/sh 
# google api requirements:
# https://developers.google.com/gdata/articles/using_cURL
# https://developers.google.com/gdata/faq#clientlogin
# https://developers.google.com/google-apps/contacts/v3/
# https://developers.google.com/google-apps/contacts/v3/reference
# json parser:
# https://github.com/dominictarr/JSON.sh#jsonsh
# recommendation(optional):
# https://developers.google.com/accounts/docs/OAuth2UserAgent

# echo "logging in to google and saving contact with given caller-number."
Auth=$(curl --silent https://www.google.com/accounts/ClientLogin --data-urlencode Email=${Email} --data-urlencode Passwd=${Passwd} -d accountType=GOOGLE -d source=Google cURL-Example -d service=cp | grep Auth | cut -d= -f2)
curl -o ${dir}/${tmpfile} --silent --header "Authorization: GoogleLogin auth=$Auth" "https://www.google.com/m8/feeds/contacts/$Email/full?v=3.0&q=$2&alt=$Format"

might help you to get to your solution ;)

2
  • looks interessting. Can you add some examples regarding the variables you use?
    – jerik
    Sep 28, 2018 at 20:40
  • 1
    @jerik: Because of security aspects, login via user and password is not supported anymore. You have to use an application-password or OAuth. Sep 29, 2018 at 21:42
0

Try looking at gmail api: http://code.google.com/apis/gmail/oauth/code.html

1
  • Good thought, but the OAuth token returned by xoauth.py doesn't work.
    – Bryon
    Jan 21, 2012 at 15:58
0

I couldn't find anything so I built a litte tool that does this. You can find it here https://github.com/gedl/gc-csv

It can export your google contacts to a comma separated file, to the console or upload it to a VOIP Phone (Incom ICW1000G)

I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.