【问题标题】:Export GMail Contacts via Unattended Script通过无人值守脚本导出 GMail 联系人
【发布时间】:2012-02-14 05:21:21
【问题描述】:

GMail 有一个方便的窗口,可让您将所有联系人导出为 CSV 文件。如果您这样做,然后查看浏览器的历史记录,您会看到用于启动下载的 URL 类似于:

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

我想用 Python 编写一个批处理脚本,它会每晚自动将此 CSV 文件下载到我的服务器,但我不知道如何获取“tok”。我研究了 Google 的 OAuth,但它似乎是一个人机交互的过程,而我需要在凌晨 3 点进行,那时所有可用的人都睡着了。

这可以做到吗,或者 Google 是否让我的电子邮件帐户变得如此“安全”,以至于我无法通过无人参与的脚本访问它?

谢谢! 布赖恩·M·埃利奥特

【问题讨论】:

  • 您可以随时使用 selenium 或其他方式:以编程方式打开浏览器窗口、登录 gmail 等。
  • 另外我认为Backupify可以做到这一点,所以肯定有可能。

标签: python gmail export contacts google-contacts-api


【解决方案1】:

首先使用您的帐户和密码进行身份验证(请参阅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`

获得神秘的“tok”变量。我发现它是 JSON 请求的一部分:

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`

下载 CSV(Google 格式)。这与手动执行此操作的方式完全相同: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"

变量“out”可以是 google_csv、outlook_csv、vcard 之一。 我的代码是 bash,您可能希望将 curl 命令更改为 Python 等效命令。重要的信息是变量和 URL。

【讨论】:

【解决方案2】:

我一直在寻找一些类似的解决方案,但我自己找不到任何现成的解决方案:(请忍受我的懒惰脚本,只是想让它工作: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"

可能会帮助您找到解决方案;)

【讨论】:

  • 看起来很有趣。你能添加一些关于你使用的变量的例子吗?
  • @jerik:出于安全考虑,不再支持通过用户名和密码登录。您必须使用an application-passwordOAuth
【解决方案3】:

【讨论】:

  • 好主意,但是 xoauth.py 返回的 OAuth 令牌不起作用。
【解决方案4】:

我找不到任何东西,所以我构建了一个小工具来执行此操作。你可以在这里找到它https://github.com/gedl/gc-csv

它可以将您的 google 联系人导出到逗号分隔的文件、控制台或上传到 VOIP 电话 (Incom ICW1000G)

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多