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

Google Gmail API, works fine as .py but throws "googleapiclient.errors.UnknownApiNameOrVersion: name: gmail version: v1" when ran as .exe

代码在 Pycharm 或 运行 .py 文件中运行完美,但我需要应用程序成为 .exe 文件才能在没有 [=] 的设备上 运行 34=].

我试图让用户在应用程序中报告来自 tkinter window 的 bug/give 反馈。然后通过 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',
                        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)

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

通过将 google-api-python-客户端恢复到 1.8.0

解决了这个问题

pip安装google-api-python-client==1.8.0

与 Google 教室 api 有完全相同的问题。以 .py 形式工作,在使用 pyinstaller 转换时不以 .exe 形式工作。完全相同的错误,除了 Google 教室 api.

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

按照此处的建议将 google-api-python-client 降级到 1.8.0 对我也有效。

这是另一个对我也有效的解决方案,似乎是更优雅的解决方案,因为不需要降级:

adding static_discovery=False in your build function solves this.

Your build function will sort of look like this build('drive', 'v3', credentials=creds, static_discovery=False)

来源:Python: Google Drive API v3 Can't Work After Converted to .exe Using Pyinstaller