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

将Tsheets API接入Google Apps Script时遇Header:Host无效值错误

解决Google Apps Script中"Attribute provided with invalid value: Header:Host"错误

这个问题的核心原因很明确:Google Apps Script的UrlFetchApp服务不允许手动设置Host请求头。Postman这类工具允许自定义Host头,但GAS会自动根据你请求的URL来生成并设置Host头,手动添加反而会触发验证错误。

具体修复步骤:

  • 从你的settings对象的headers里移除"Host": "rest.tsheets.com"这一行
  • 顺便清理重复的cache-control头(你现在有两个,虽然不触发错误,但留一个就够了)

修改后的完整代码:

function fetch() {
  var settings = { 
    "async": true, 
    "crossDomain": true, 
    "method": "GET", 
    "headers": { 
      "Authorization": "Bearer "+token, 
      "User-Agent": "PostmanRuntime/7.20.1", 
      "Accept": "*/*", 
      "Cache-Control": "no-cache", 
      "Postman-Token": postman_token, 
      "Accept-Encoding": "gzip, deflate", 
      "Connection": "keep-alive" 
    } 
  }
  var now = new Date();
  var day = now.getDate()>9? now.getDate():'0'+now.getDate();
  // 注意:getMonth()返回0-11,需加1才是实际月份
  var date = now.getFullYear()+'-'+(now.getMonth()+1)+'-'+day;
  var url = organization+'/timesheets?start_date='+date;
  Logger.log(url);
  var response = UrlFetchApp.fetch(url,settings);
  Logger.log(response);
}

额外提醒:

我还注意到你的日期生成有个小问题——getMonth()返回的是0到11的数值(比如1月是0,12月是11),所以需要加1才能得到正确的月份值,上面的代码已经帮你修正了这个细节,避免生成类似2024-0-15这种无效日期。

另外,UrlFetchApp还有几个默认会自动处理的请求头,比如Content-Length、某些场景下的Cookie,也不建议手动设置,否则可能会触发类似的错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:50:36