【问题标题】:run_flow complains about getting its minimum three argumentsrun_flow 抱怨得到它的最少三个参数
【发布时间】: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


【解决方案1】:

在使用 run_flow python 时,您不需要向命令行传递额外的参数。

import argparse
...
from oauth2client import tools
...
from oauth2client.tools import run_flow
...
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
....
credentials = run_flow(flow, STORAGE, flags, http=http)

然后就可以运行了

python quickstart.py

【讨论】:

  • 这导致我在登录后出现 400 错误,但至少它是进步的。看起来我需要匹配redirect_uris。谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 2018-06-25
  • 1970-01-01
  • 2015-07-02
  • 2013-09-16
相关资源
最近更新 更多