这个答案怎么样?请把这当作几个可能的答案之一。
问题和解决方法:
我认为你的问题是由于
payload
对于
POST
GET
方法。在UrlFetchApp中,当数据用作
有效载荷
,即使在
method
得到
. 为了将数据作为GET方法发送,请将它们添加到查询参数中。
有效载荷
使用GET方法,数据可以作为
得到
以前。但关于这件事,我只有一点模糊的记忆。对此我深表歉意。)
模式1:
在此模式中,查询参数用于GET方法。
function postit() {
var ask=SpreadsheetApp.getUi().prompt("Enter first/second/third/method", SpreadsheetApp.getUi().ButtonSet.OK)
if(ask.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
var dA=ask.getResponseText().split('/');
if(dA.length==4) {
var url=ScriptApp.getService().getUrl();
var potions = {};
if (dA[3] == "POST") {
var data={"one":dA[0],"two":dA[1],"three":dA[2]};
options={"method":dA[3],"payload":data,"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
} else if (dA[3] == "GET") {
options={"method":dA[3],"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
url += `?one=${dA[0]}&two=${dA[1]}&three=${dA[2]}`;
}
var resp=UrlFetchApp.fetch(url, options);
Logger.log('UrlFetch Response: %s',resp);
}
}
}
模式2:
在这种模式下,作为另一种解决办法,
x-http-method-override
x-http-method-override
在这种情况下,POST方法可以转换为GET方法。所以
"payload":data
也可以使用。
修改脚本:
function postit() {
var ask=SpreadsheetApp.getUi().prompt("Enter first/second/third/method", SpreadsheetApp.getUi().ButtonSet.OK)
if(ask.getSelectedButton()==SpreadsheetApp.getUi().Button.OK) {
var dA=ask.getResponseText().split('/');
if(dA.length==4) {
var url=ScriptApp.getService().getUrl();
var potions = {};
if (dA[3] == "POST") {
var data={"one":dA[0],"two":dA[1],"three":dA[2]};
options={"method":dA[3],"payload":data,"headers":{"Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
} else if (dA[3] == "GET") {
options={"method":dA[3],"payload":data,"headers":{"x-http-method-override": "POST", "Authorization":"Bearer " + ScriptApp.getOAuthToken()},"muteHttpExceptions": true};
}
var resp=UrlFetchApp.fetch(url, options);
Logger.log('UrlFetch Response: %s',resp);
}
}
}
注:
-
请启用V8。
-
// DriveApp.getFiles()
https://www.googleapis.com/auth/drive.readonly
使用脚本编辑器。这用于使用访问令牌访问Web应用程序。
参考文献:
如果我误解了你的观点,这不是你想要的方向,我道歉。