我在尝试让ezgmail工作时遇到pydevconsole错误。我怎样才能解决这个问题?

2024-09-24 04:23:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图初始化ezgmail,但它在查找token.json文件时遇到了问题。它首先发出警告,无法找到token.json文件,然后出现如下错误并挂起。我遵循的流程(我在PyCharm的venv工作)如下:

随后的程序: 登录谷歌并启用Gmail API;改名为"邮件",; 配置您的Oauth客户端:我选择了“桌面应用程序” 将生成的credentials.json文件复制到项目文件夹中

我甚至将quickstart.py文件复制到我的工作目录中,并毫无问题地运行它(python quickstart.py)。 然后: (在Python控制台:)::

>>>import os
>>>

我得到的错误如下所示:

import ezgmail
usage: pydevconsole.py [--auth_host_name AUTH_HOST_NAME]
                       [--noauth_local_webserver]
                       [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                       [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
pydevconsole.py: error: unrecognized arguments: --mode=client --port=60056

Process finished with exit code 2

如有任何见解,将不胜感激,如有帮助,欢迎提供更多信息。干杯(我是新手,请温柔对待!)


Tags: 文件pyimporttokenauthjsonhostport
3条回答

显然this post帮助我进步了一点(仍然卡住了,但更进一步!)。这意味着我必须修改pip安装的一个软件包。(oauth2client.tools)。所以我会关闭这个,除非其他人有任何见解

将您的凭证json文件重命名为credentials-gmail.json(如果还没有的话)(您应该在根目录中有此文件)

如果没有您试图调试的实际代码,就很难回答这个问题。但这里有一些建议。如果您已经为Gmail API配置并下载了访问令牌,请尝试使用绝对路径而不是相对路径

如果您在生成令牌时遇到问题,可以尝试从命令行运行python代码,这样它将使用默认浏览器将您带到授权屏幕。我很难让jupyter笔记本使用我的浏览器打开授权屏幕

以下代码来自quickstart.py,它应该向您展示如何首先生成token.json:

def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

service = build('gmail', 'v1', credentials=creds)

相关问题 更多 >