简体   繁体   English

将 Tsheets api 连接到 Google 应用程序脚本时出错

[英]Error connecting Tsheets api to Google apps script

When running the script below from the GAS environment, I get the error message: "Attribute provided with invalid value: Header:Host"从 GAS 环境运行以下脚本时,我收到错误消息:“属性提供无效值:Header:Host”

 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, "Host": "rest.tsheets.com", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "cache-control": "no-cache" } } var now = new Date(); var day = now.getDate()>9? now.getDate():'0'+now.getDate(); var date = now.getFullYear()+'-'+now.getMonth()+'-'+day; var url = organization+'/timesheets?start_date='+date; Logger.log(url); var response = UrlFetchApp.fetch(url,settings); Logger.log(response); }

The original code was generated using Postman and works with no issues but I translated it from AJAX syntax to GAS and it doesn't work from the GAS environment.原始代码是使用 Postman 生成的,并且没有问题,但我将其从 AJAX 语法转换为 GAS,并且在 GAS 环境中不起作用。

Delete the Host from the header fields, it gets specified automatically and fetch from Apps Script won't allow you to specify it, just delete it, because it is what is rising the error.从标题字段中删除Host ,它会自动指定并且从 Apps 脚本中获取不允许您指定它,只需将其删除,因为它是导致错误的原因。

About the rest:关于其余:

Postman-token is useless here, you can delete it also. Postman-token在这里没用,你也可以删除它。 Same with user-agent , I believe it will be overwritten, so it can be safely ignored.user-agent相同,我相信它会被覆盖,因此可以安全地忽略它。

Finally, parameters as crossDomain and async although they won't rise any exception, they are also ignored, so you can just suppress them.最后, crossDomainasync参数虽然不会引发任何异常,但它们也会被忽略,因此您可以直接抑制它们。

On here you'll find documentation about the allowed parameters for the Class UrlFetchApp , and on here you'll find the w3c documentation on the header field definitions.此处您将找到有关Class UrlFetchApp允许参数的Class UrlFetchApp ,在此处您将找到有关标头字段定义的 w3c 文档。

This should do:这应该做:

var settings = {
    "method": "GET",
    "headers": {
      "Authorization": "Bearer "+token,
      "Accept": "*/*",
      "Cache-Control": "no-cache",
      "Accept-Encoding": "gzip, deflate",
      "Connection": "keep-alive",
      "cache-control": "no-cache"
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM