Google Gmail API,作为 .py 工作正常,但在作为 .exe 运行时抛出“googleapiclient.errors.UnknownApiNameOrVersion: name: gmail version: v1”

问题描述

代码在 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 示例。

代码片段:

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',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)

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

解决方法

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

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

,

与 Google 课堂 api 完全相同的问题。作为 .py 工作,在使用 pyinstaller 转换时无法作为 .exe 工作。完全相同的错误,除了 Google 课堂 api。

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