json - 如何将参数传递给已部署的 Google Apps 脚本 API 函数?
问题描述
我使用 Google Cloud Platform (GCP) 在 Google Apps Script API 中创建了一个测试函数 (doPost)。我现在正试图从同一个项目中的另一个脚本调用该函数。
我知道我快到了,因为这段代码有效:
var token = ScriptApp.getOAuthToken();
var header = {
"Authorization": "Bearer " + token,
"function": "doPost",
"devMode": true,
};
var options = {
"method": "POST",
"headers": header,
"muteHttpExceptions": true,
"payload": {
"function": "doPost",
"devMode": true
}
};
var url = 'https://script.googleapis.com/v1/scripts/xxxxxxxxxxxxxxxxxxxx:run';
var response = UrlFetchApp.fetch(url, options);
但是,当我尝试在上面的有效负载中包含一个参数时,它不再起作用:
"payload": {
"function": "doPost",
"parameters": ['1'],
"devMode": true
}
在其他stackoverflow答案之后,我尝试在标题中使用:
"contentType": 'application/json',
因此,对于有效载荷:
"payload": JSON.stringify({
"function": "doPost",
"parameters": ['1'],
"devMode": true
})
每当我使用“JSON.stringify”时,即使没有参数(就像我开始工作的情况一样),它也会出错。
使用 JSON.stringify (以及有效负载中的参数),我得到一个更严重的错误,这似乎表明它不喜欢任何有效负载:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message."
如果没有 JSON.stringify (并在有效负载中使用参数),我会收到错误消息:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types."
最后,我对参数做了什么并不重要。我很确定它应该采用我上面的格式,但我也尝试过:
"parameters": [1]
"parameters": 1
"parameters": "1"
其中。
doPost 脚本现在很简单:
function doPost(parameters) {
Logger.log('parameters = ' + parameters);
return "Hello";
}
这是似乎最像这样的stackoverflow问题:Apps Script API returned 404 error for existing project。错误返回为 HTML 而不是 JSON,但似乎没有回答我的问题。
我研究了有关参数的 scripts.run 页面:https ://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#authorization-scopes以及许多其他页面,包括URL 获取服务:https://developers.google.com/apps-script/reference/url-fetch。
这当然不是我第一次在 Google Apps 脚本中使用 UrlFetchApp,但这是在调用我自己的 GAS API 时。
任何帮助将不胜感激!
谢谢!
解决方案
在我发布这篇文章后不久,在继续看到一个又一个帖子后说你应该在标题中使用 JSON.stringify 和 contentType: application/json ,在标题中我改变了:
"contentType": "application/json"
至
"Content-Type": "application/json"
现在可以了!
推荐阅读
- c# - 确保在 UI 线程上创建新表单
- video - 什么是 I 帧,它们会影响视频大小吗?
- angular - TypeError: changes.payload.docChanges is not a function in Angular 6 using AngularFire2 v5-rc9
- android - TexInputLayout 错误消息未设置
- selenium - 每次节点被杀死时,Selenium Grid 都会重新启动
- php - Foreach ,并通过 ID 获取名称不返回任何内容
- r - R合并循环
- python - 函数中的Python变量重新定义
- javascript - 反应在单击时复制呈现的 div
- vba - 使用 TempVars 去记录