【发布时间】:2017-10-16 14:47:56
【问题描述】:
我正在尝试为我正在设置的 Python 微服务设置一个带有服务器到服务器身份验证的 Google 应用脚本 API 可执行文件。
使用快速入门,我能够通过 Auth2 使其工作,但我无法使用服务帐户使其工作。我授予对服务帐户电子邮件的脚本和电子表格的访问权限。客户端密钥 JSON 中的项目 ID 与应用脚本的项目 ID 匹配。我同样将它部署为 API 可执行文件。
这是我下面的代码(虽然我不认为代码是问题):
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from googleapiclient.discovery import build
scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/script.external_request',
'https://www.googleapis.com/auth/script.storage',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/userinfo.email'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scopes)
http_auth = credentials.authorize(Http())
service = build('script', 'v1', http=http_auth)
request = {'function': 'testApi'}
response = service.scripts().run(body=request, scriptId='SCRIPT_ID').execute()
print(response)
我的应用脚本中的 testApi 函数是一个返回“It works”的简单函数。
我不断收到用户在使用个人帐户时没有权限 (403),在使用组织(G Suite 帐户)时甚至是 500。
如前所述,Google 文档中的快速入门教程有效,但这并没有使用服务帐户。
有没有人让 Google Apps Scripts API 可执行,与服务器到服务器的身份验证帐户流一起工作?
【问题讨论】:
-
您的服务帐户是否在管理控制台中具有 API 客户端访问权限? (安全 > 高级设置 > 管理 API 客户端访问)如果没有,您需要添加项目的服务帐户凭据并授予其访问适当范围的权限。
标签: python python-3.x google-apps-script google-api