【发布时间】:2014-12-15 08:54:34
【问题描述】:
我正在编写一个通过 GMail API 发送电子邮件的简单脚本,但我发现用于访问其 SMTP 接口的旧脚本无法正常工作。
所以我使用他们quickstart page 中的以下脚本首先阅读:
#! /usr/bin/env python
#
import httplib2
from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
CLIENT_SECRET = '.client.json'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly'
STORAGE = Storage('gmail.storage')
flow = flow_from_clientsecrets(CLIENT_SECRET, scope=OAUTH_SCOPE)
http = httplib2.Http()
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = run(flow, STORAGE, http=http)
http = credentials.authorize(http)
gmail_service = build('gmail', 'v1', http=http)
threads = gmail_service.users().threads().list(userId='me').execute()
if threads['threads']:
for thread in threads['threads']:
print 'Thread ID: %s' % (thread['id'])
运行它会得到一个NotImplementedError,如this question所示。
所以我导入并调用了run_flow 而不是run,因为我没有安装gflags 来继续。但是,我收到以下错误:
TypeError: run_flow() takes at least 3 arguments (3 given)
我从链接的问题中了解到argparse 应该会有所帮助。我可以将调用添加到该问题使用的parser,但我不知道要在命令行上传递哪些参数。
任何人成功地用这个实现了一些可以提供帮助的东西吗?
【问题讨论】:
-
不确定“他们已禁用 SMTP 接口”是什么意思,但我向您保证,Google 并未禁用他们的 SMTP 接口。 :)
-
嗯,哎呀当我写的时候我累了:P 我想我的意思是说一个使用 libsmtp 连接到 Google 的旧 Python 脚本不再工作了。 :P
标签: python-2.7 google-oauth google-api-python-client gmail-api