具有服务帐户的App脚本执行API-HTTP 400无效参数 [英] App Script Execution API with Service account - HTTP 400 Invalid arguments
问题描述
我正在从Python脚本运行Google App脚本可执行API.我正在使用服务帐户来执行此操作,以便可以安排它进行无人值守运行.
I am running the Google App script executable API from a Python script. I am doing this using a service account so that it can be scheduled for unattended run.
我尝试使用自己的帐户执行此操作,OAuth通过打开浏览器页面并请求授权来工作.我授权后执行脚本.我已按照以下链接中的建议将范围授权给服务帐户
I have tried to do this using my own account and OAuth works by opening up a browser page and asking for authorization. After I authorize the script is executed. I have authorized the scopes to the service account as suggested in the below link
https://developers.google.com/identity/protocols/OAuth2ServiceAccount
from __future__ import print_function
from googleapiclient import errors
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
from google.oauth2 import service_account
import googleapiclient.discovery
import httplib2
import json
SCRIPT_ID ='xyz'
SCOPES = ['https://www.googleapis.com/auth/documents' ,'https://www.googleapis.com/auth/forms', 'https://www.googleapis.com/auth/presentations','https://www.googleapis.com/auth/spreadsheets']
service_account_info = json.load(open('service_account_key_file.json'))
creds = service_account.Credentials.from_service_account_info( service_account_info)
service = build('script', 'v1', credentials=creds)
request = {"function": "testExecutableSA","parameters": ["TEST RUN"]}
try:
response = service.scripts().run(body=request,
scriptId=SCRIPT_ID).execute()
currentdata = response
print(currentdata)
except errors.HttpError as e:
# The API encountered a problem before the script started executing.
print(e.content)response = service.scripts().run(body=request, scriptId=SCRIPT_ID).execute()
当我使用我的帐户进行操作时,脚本会运行,并且会通过名为这是测试运行""的App脚本记录一条测试消息
When I do it using my account the script runs and a test message is logged via the App script that is being called "This is a "Test Run"
使用服务帐户文件执行此操作时,出现以下错误
When I do it using the service account file I get the below error
b'{\ n错误":{\ n代码":400,\ n消息":请求包含无效的参数.",\ n错误":[\ n {\ n消息:"请求包含无效的参数.,\ n"域:"全局,\ n"原因:" badRequest"\ n} \ n],\ n"状态:" INVALID_ARGUMENT"\ n} \ n} \ n'
b'{\n "error": {\n "code": 400,\n "message": "Request contains an invalid argument.",\n "errors": [\n {\n "message": "Request contains an invalid argument.",\n "domain": "global",\n "reason": "badRequest"\n }\n ],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
没有关于无效参数的详细信息.
There is no detail on what is the invalid argument.
唯一改变的是传递的http_authorized
对象.
Only this that changes is the http_authorized
object that is passed.
我犯错了还是发生了故障?
Am I making a mistake or something is broken ?
推荐答案
根据官方文档 Apps Script API不适用于服务帐户.
According to the official documentation the Apps Script API does not work with service accounts.
这篇关于具有服务帐户的App脚本执行API-HTTP 400无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!