Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export GMail Contacts via Unattended Script

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

like image 787
Bryon Avatar asked Jan 19 '12 14:01

Bryon


2 Answers

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 ;)

like image 26
Christoph Lösch Avatar answered Nov 08 '22 13:11

Christoph Lösch


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

auth=`curl --silent -d [email protected] -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.

like image 122
SoftIce Avatar answered Nov 08 '22 13:11

SoftIce