【问题标题】:Google Gmail API, works fine as .py but throws "googleapiclient.errors.UnknownApiNameOrVersion: name: gmail version: v1" when ran as .exeGoogle Gmail API,作为 .py 运行良好,但在作为 .exe 运行时抛出“googleapiclient.errors.UnknownApiNameOrVersion: name: gmail version: v1”
【发布时间】:2021-06-16 09:27:58
【问题描述】:

代码在 Pycharm 中完美运行,或者在运行 .py 文件时,但我需要将应用程序作为 .exe 文件才能在没有 python 的设备上运行。

我正在尝试允许用户从 tkinter 窗口报告应用程序中的错误/提供反馈。然后通过 gmail-api 将反馈发送给我。

.exe 文件由 pyinstaller 生成(从虚拟环境内部运行) 运行 exe 文件时一切正常,直到:

service = build(serviceName='gmail',
                        version='v1',
                        credentials=creds,
                        discoveryServiceUrl="https://gmail.googleapis.com/$discovery/rest?version=v1")

在哪里生产

  File "googleapiclient\_helpers.py", line 134, in positional_wrapper
  File "googleapiclient\discovery.py", line 273, in build
  File "googleapiclient\discovery.py", line 387, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: gmail  version: v1

当点击 tkinter 按钮时执行下面的代码。

gmail 代码几乎完全复制自 google gmail api 示例。

小代码sn-p:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
def process_report():
        creds = None
        if os.path.exists('token.json'):
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        # 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(
                    'client_id.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.json', 'w') as token:
                token.write(creds.to_json())

        service = build(serviceName='gmail',
                        version='v1',
                        credentials=creds,
                        discoveryServiceUrl="https://gmail.googleapis.com/$discovery/rest?version=v1")

        msg = create_message("sender_email@gmail.com",
                       "reciever_email@other.com",
                       subject, message)

        send_message(service, "me", msg)

非常感谢任何帮助或建议。

【问题讨论】:

  • 您使用的是什么类型的凭据?
  • 激活gmail api时下载的client_id.json文件。遵循谷歌 api 示例步骤。包含 client_id、project_id、auth_uri、token_uri、auth_provider_x509_cert_url 和 client_secret
  • 适用于什么类型的应用程序?
  • 桌面应用程序

标签: python google-api gmail gmail-api


【解决方案1】:

通过将 google-api-python-client 恢复为 1.8.0 解决了该问题

pip install google-api-python-client==1.8.0

【讨论】:

    【解决方案2】:

    在使用 Google 课堂 API 时遇到了完全相同的问题。作为 .py 工作,使用 pyinstaller 转换时不能作为 .exe 工作。完全相同的错误,除了 Google 课堂 API。

    完全相同的解决方案有效(还原 google-api-python-client)。我会发表评论,但我没有足够的评论积分。

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2021-10-09
      • 1970-01-01
      • 2016-01-15
      • 2019-09-29
      • 2020-03-09
      • 1970-01-01
      • 2019-11-17
      • 2016-06-04
      相关资源
      最近更新 更多