微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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)。我会发表评论,但我没有足够的分数来评论。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。