首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >,作为"googleapiclient.errors.UnknownApiNameOrVersion:.py运行良好,但抛出Gmail名称: gmail版本: v1“时运行为.exe

,作为"googleapiclient.errors.UnknownApiNameOrVersion:.py运行良好,但抛出Gmail名称: gmail版本: v1“时运行为.exe
EN

Stack Overflow用户
提问于 2021-03-18 22:53:03
回答 3查看 1.3K关注 0票数 4

代码在Pycharm中或在运行.py文件时运行得很好,但我需要该应用程序成为一个.exe文件,以便在没有python的设备上运行。

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

.exe文件是由pyinstaller (从虚拟环境中运行)生成的,当运行exe文件时,所有操作都正常,直到:

代码语言:javascript
运行
复制
service = build(serviceName='gmail',
                        version='v1',
                        credentials=creds,
                        discoveryServiceUrl="https://gmail.googleapis.com/$discovery/rest?version=v1")

它产生的地方

代码语言:javascript
运行
复制
  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示例。

小代码片段:

代码语言:javascript
运行
复制
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)

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-01 04:54:10

通过将google python-客户机恢复为1.8.0解决了这个问题。

pip安装google=1.8.0

票数 5
EN

Stack Overflow用户

发布于 2021-05-07 04:14:47

谷歌课堂api也有同样的问题。作为.py工作,在与pyinstaller转换时不作为.exe工作。完全相同的错误,除了谷歌教室api。

完全相同的解决方案起作用了(还原)。我本想发表评论的,但我没有足够的观点发表评论。

票数 2
EN

Stack Overflow用户

发布于 2022-05-24 16:24:18

按照这里的建议,将降级为1.8.0也对我有帮助。

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

在构建函数中添加static_discovery=False解决了这个问题。

您的构建函数将类似于此构建(“驱动器”、“v3”、credentials=creds、static_discovery=False)

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66700249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档