我在将 API 连接到 Google 表格时遇到问题。
使用此获取/请求,我如何使用 Apps 脚本连接到 Google 表格?
const options = {
method: 'GET',
headers: {
accept: 'application/json',
'X-API-KEY': 'MyKey'
}
};
fetch('https://api.whip-around.com/api/public/v4/work-orders?limit=20&page=1', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
我在 Google Apps 脚本中尝试过此操作,但它不起作用
function fetchDataFromAPI() {
var apiUrl = 'https://api.whip-around.com/api/public/v4/work-orders';
var headers = {
'Authorization': 'Bearer MyKey'
};
var options = {
'headers': headers
};
var response = UrlFetchApp.fetch(apiUrl, options);
var responseData = JSON.parse(response.getContentText());
// Now you can manipulate the API data and update your Google Sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var numRows = responseData.length;
var numCols = responseData[0].length;
sheet.getRange(1, 1, numRows, numCols).setValues(responseData);
}
我相信您的目标如下。
如果您的 Javascript 工作正常并且您想从 Javascript 转换为 Google Apps 脚本,我认为需要在请求标头中包含
accept: 'application/json', 'X-API-KEY': 'MyKey'
而不是 'Authorization': 'Bearer MyKey'
。
关于您的以下回复,
这就是你所说的改变吗?函数 fetchDataFromAPI() { var apiUrl = 'https://api.whip-around.com/api/public/v4/work-orders'; var headers = { 接受: 'application/json','mykey' }; var options = { 'headers': headers };另外,这里是文档的链接:whiparound-public-api.readme.io/reference/…
既然如此,下面的修改如何?
function fetchDataFromAPI() {
var yourAPIkey = "###"; // Please set your API key.
var apiUrl = 'https://api.whip-around.com/api/public/v4/work-orders?limit=20&page=1';
var options = { headers: { "X-API-KEY": yourAPIkey, accept: "application/json" } };
var response = UrlFetchApp.fetch(apiUrl, options);
var responseData = response.getContentText();
console.log(responseData); // Here, you can see the response value in the log.
}