我在 Google Apps Script 中创建了函数,当我在 Google Apps Script 中运行它时效果很好。输出数据返回到 Google 表格。
function testFunction11() {
var rng = SpreadsheetApp.getActiveRange();
var encodedAuthInformation = Utilities.base64Encode("username:key");
var headers = {"Authorization" : "Basic " + encodedAuthInformation};
var params = {
'method': 'GET',
'muteHttpExceptions': true,
'headers': headers
};
var res = UrlFetchApp.fetch("https://api.apiservice.com/api/v1/xxx?fields=somefields", params);
Logger.log(res.getContentText());
rng.setValue(res);
}
Run Code Online (Sandbox Code Playgroud)
单元格中的输出:
[
{
"id": xxx,
"createdDate": "2019-02-01T04:54:00Z",
"reference": "XXX"
},
etc
Run Code Online (Sandbox Code Playgroud)
然后我将脚本分配给按钮“testFunction11”。当我点击按钮时,它返回
{
"message": "An error has occurred."
}
Run Code Online (Sandbox Code Playgroud)
它看起来像来自 API 服务器的响应。
我唯一的假设是 google sheet 的按钮添加了一些 headers,User-Agent 或 content-type 来请求,这在 …
get basic-authentication urlfetch google-apps-script x-forwarded-for