Google 应用程序脚本 - “消息”:“未找到请求的实体。” devMode = false

eve*_*der 3 python google-apps-script google-cloud-platform

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

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

devMode当我用as传递请求时true,一切正常。据我所知,在这种情况下,它正在运行最新保存的版本。但是,当我将其设置为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()
Run Code Online (Sandbox Code Playgroud)

Kes*_*ssy 6

我遇到了同样的问题,并且发现将脚本 ID 更改为部署 ID 是有效的。

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

  • 打开脚本
  • 转到Deploy->Manage Deployments
  • 从活动部署中获取部署 ID

部署ID

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