0

在尝试使用 Python Google API 客户端测试 Execution API 时,我们目前只有一个输出字符串的函数,我们似乎得到了带有 message 的 403 "The caller does not have permission"。我们能够根据快速入门让库与创建新脚本一起工作。其他人也问过类似的问题,例如:

为什么我的应用程序脚本部署为 API 可执行文件返回 Permission Denied?

但是我看到的一个常见线程是脚本本身应该将控制台项目链接为 by Resources > Developer Console Project,但现在已更改为Resources > Cloud Platform Project,并且将 ID 更改为属于我们使用 OAuth2 凭据拥有的控制台项目的 ID 不起作用,因为它似乎认为该项目不存在。在偶然发现该线程之前,我们甚至不需要挂钩到云平台来执行scripts.run. fwiw 我们还在控制台项目上启用了 Apps Script API。

此处包含的代码(半成品,所以有一个 for 循环,它只使用硬编码的脚本 ID 执行一次):

from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from pprint import pprint
from time import sleep

SCOPES = ['https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/forms']

if __name__ == '__main__':
    store = file.Storage('token-app.json')
    creds = store.get()

    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)

    store_cloud = file.Storage('token-cloud.json')
    creds_cloud = store_cloud.get()

    if not creds_cloud or creds_cloud.invalid:
        flow = client.flow_from_clientsecrets('credentials-cloud.json', SCOPES)
        creds_cloud = tools.run_flow(flow, store_cloud)

    service = build('drive', 'v3', http=creds.authorize(Http()))
    apps_service = build('script', 'v1', http=creds_cloud.authorize(Http()))

    results = service.files().list(
        q=("mimeType='application/vnd.google-apps.form'"), pageSize=10, 
            fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            # print(item.keys())
            print(u'{0} ({1})'.format(item['name'], item['id']))
            script_id = 'SCRIPT_ID'

            try:
                response = apps_service.scripts().run(scriptId=script_id, 
                    body={'function': 'myFunction'}).execute()
                pprint(response.to_json())
            except errors.HttpError as error:
                pprint(error.content)

关于在哪里查看或更改为非常感谢的任何建议,因为这个问题似乎已经在几年前得到了回答,所以猜测的东西已经改变了一点?

4

0 回答 0