You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTML POST请求调用Google Sheets API时遭遇404错误求助

解决Google Sheets API POST请求404错误的问题

你的404错误大概率是URL格式错误导致的,我帮你拆解问题和修复方案:

1. 缺失关键斜杠,导致路径无效

看你代码里的URL拼接部分:

httpRequest.open('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheetID + 'values/Sheet1!B2:L2:append', true)

在spreadsheetID和values之间少了一个斜杠,Google Sheets API的追加端点标准格式是:
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append
所以正确的拼接应该补上这个斜杠:

httpRequest.open('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheetID + '/values/Sheet1!B:L:append', true)

2. Range范围写法优化(避免后续潜在问题)

你当前写的Sheet1!B2:L2是单个单元格范围,追加数据时更合理的写法是指定列范围(比如Sheet1!B:L),这样API会自动在指定列的下一行追加新数据。如果锁定到具体单元格,可能出现数据覆盖或追加位置不符合预期的问题。

修复后的完整代码片段

var httpRequest = new XMLHttpRequest(); 
httpRequest.onreadystatechange = function() { 
  if (httpRequest.readyState == XMLHttpRequest.DONE) { 
    // 建议打印responseText,方便查看具体错误信息
    console.log(httpRequest.status, httpRequest.responseText);
    alert(httpRequest.status); 
  } 
} 
var params = { "values": [ [ "value1", "value2" ] ] } 
// 修复URL的斜杠和range范围
httpRequest.open('POST', 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheetID + '/values/Sheet1!B:L:append', true) 
httpRequest.setRequestHeader('Authorization', 'Bearer ' + accessToken); 
httpRequest.setRequestHeader('Content-Type', 'application/json'); 
httpRequest.send(JSON.stringify(params));

另外,建议你在请求回调里打印httpRequest.responseText,API返回的错误详情会比单纯的状态码更有助于排查问题。

内容的提问来源于stack exchange,提问作者mistry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:46