python - 谷歌应用程序脚本 - "message": "Requested entity was not found." with devMode = false

标签 python google-apps-script google-cloud-platform

我正在尝试将我的 Python 脚本与我在 Google App 脚本中的项目连接起来。我已遵循此 guide 中的所有说明.

当然,我已将其部署为可执行 API,并在仅允许我自己、我的组织和任何人访问的情况下对其进行了测试。

当我以 devModetrue 传递请求时,一切正常。我知道在这种情况下,它运行的是最新保存的版本。但是,当我将其设置为 false 时,我会返回错误 "message": "Requested entity was not found." 据我所知,它正在尝试运行最新部署的版本。

我也尝试过这些问题 12但显然,他们遇到的问题恰恰相反,脚本不会在 devMode 设置为 true 的情况下运行。

其他一切似乎都在正确执行,但我找不到不在 devMode 上就无法运行脚本的原因

这是我的脚本:

"""
Shows basic usage of the Apps Script API.
Call the Apps Script API to create a new script project, upload a file to the
project, and log the script's URL to the user.
"""
from __future__ import print_function
import pickle
import os.path
from googleapiclient import errors
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/script.projects',
          'https://www.googleapis.com/auth/spreadsheets.currentonly'
          ]

SCRIPT_ID = 'someid'


def main():
    """Calls the Apps Script API.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # 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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('script', 'v1', credentials=creds)

    # Call the Apps Script API
    try:
        # Create a new project
        request = {'function': 'setData'}
        response = service.scripts().run(body=request,
                                         scriptId=SCRIPT_ID).execute()

        print(response)
    except errors.HttpError as error:
        # The API encountered a problem.
        print(error.content)


if __name__ == '__main__':
    main()

最佳答案

我曾经遇到过同样的问题,而且我发现将脚本 ID 更改为部署 ID 是可行的。

可以在 Apps Script 脚本中找到部署 ID:

  • 打开脚本
  • 转到部署 -> 管理部署
  • 从事件部署中获取部署 ID

Deployment ID

部署完成后,转到 python 脚本并使用部署 ID 修改 SCRIPT ID

关于python - 谷歌应用程序脚本 - "message": "Requested entity was not found." with devMode = false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65773410/

相关文章:

.net - F# 与 IronPython : When is one preferred to the other?

javascript - 有没有办法确定在 Google 表格脚本中与 onEdit 一起使用的编辑类型?

google-cloud-platform - Terraform:如何在不锁定自己的情况下使用 iam_policy

Python 3 绑定(bind)方法订阅

python - 字典 View 对象与集合

Python 的 "re"模块不工作?

google-apps-script - 连接 Google 表格中的两个范围

google-apps-script - 如何从应用程序脚本使用 Google 表格查询或 Google 可视化 API?

google-app-engine - Gradle:通过测试配置部署到Appengine的任务?

google-cloud-storage - Google Cloud Storage 基于 IP 地址的文件访问策略