<p>这很痛苦,但我现在好像有点事。。。</p>
<h3>还不支持Python3</h3>
<p>我不认为这太难实现,因为我在转换包时遇到了障碍,没有碰到任何大的问题:只有通常的2to3东西。但几个小时后,我厌倦了逆流而上游泳。在撰写本文时,我找不到Python 3的公开使用包。python 2的体验是直截了当的(相比之下)。</p>
<h2>浏览谷歌网站是成功的一半</h2>
<p>毫无疑问,随着时间的推移,这种情况将会改变。最终,您需要下载一个<code>client_secret.json</code>文件。您只能(可能)通过web浏览器进行设置:</p>
<ol>
<li>你需要一个谷歌帐户-要么谷歌应用程序或gmail。所以,如果你没有,去买一个。</li>
<li>去<a href="https://console.developers.google.com/" rel="noreferrer">developers console</a></li>
<li>创建一个新项目,并等待4或400秒才能完成。</li>
<li>导航到<code>API's and Auth</code>->;<code>Credentials</code></li>
<li>在<code>OAuth</code>下选择<code>Create New Client ID</code></li>
<li>选择<strong><code>Installed Application</code></strong>作为应用程序类型,选择<strong>其他</li>
<li>现在应该有一个按钮<code>Download JSON</code>。做那件事。这是你的<code>client_secret.json</code>-可以说是密码</li>
</ol>
<p><strong>但等等,还不止这些!</strong></p>
<p>你必须给你的申请一个“产品名”,以避免一些奇怪的错误。(看看我为给你这个付出了多少;-)</p>
<ol>
<li>导航到<code>API's & auth</code>->;<code>Consent Screen</code></li>
<li>选择您的电子邮件</li>
<li>输入产品名称。不管是什么。”Foobar“会很好的。</li>
<li>保存</li>
</ol>
<p><strong>新闻快报!哇哦。现在还有更多!</strong></p>
<ol>
<li>导航到API的&;身份验证->;API->;Gmail API</li>
<li>单击“启用API”按钮</li>
</ol>
<p>是的。现在我们可以更新电子邮件脚本。</p>
<h2>Python2</h2>
<p>您需要在第一次以交互方式运行脚本。它将在您的计算机上打开一个web浏览器,您将授予权限(单击按钮)。此练习将向您的计算机保存一个包含可重用令牌的文件<code>gmail.storage</code>。</p>
<p>[我没有运气将令牌转移到没有图形浏览器功能的机器上返回HTTPError。我试着通过lynx图形浏览器来浏览。这也失败了,因为谷歌已经将最后的“接受”按钮设置为“禁用”!?我会提出另一个问题来跳过这个障碍(更多的抱怨)]</p>
<p>首先,您需要一些库:</p>
<pre><code>pip install --upgrade google-api-python-client
pip install --upgrade python-gflags
</code></pre>
<ul>
<li>您需要更改往返地址</li>
<li>确保您拥有客户机令牌.json文件,只要<code>Storage</code>指令要求它</li>
<li>目录需要是可写的,这样它才能保存<code>gmail.storage</code>文件</li>
</ul>
<p>最后是一些代码:</p>
<pre><code>import base64
import httplib2
from email.mime.text import MIMEText
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
# Path to the client_secret.json file downloaded from the Developer Console
CLIENT_SECRET_FILE = 'client_secret.json'
# Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.compose'
# Location of the credentials storage file
STORAGE = Storage('gmail.storage')
# Start the OAuth flow to retrieve credentials
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# create a message to send
message = MIMEText("Message goes here.")
message['to'] = "yourvictim@goes.here"
message['from'] = "you@go.here"
message['subject'] = "your subject goes here"
body = {'raw': base64.b64encode(message.as_string())}
# send it
try:
message = (gmail_service.users().messages().send(userId="me", body=body).execute())
print('Message Id: %s' % message['id'])
print(message)
except Exception as error:
print('An error occurred: %s' % error)
</code></pre>
<p>希望这能让我们都开始。不像以前那样简单,但现在看起来确实不那么复杂了,我可以从肉体上看出来。</p>