Google App Script:带标题的URLFetchApp.fetch

use*_*253 3 javascript api google-apps-script

我正在尝试从rainforestqa API获取数据,但为了获得访问权限,我需要将我的api_key作为标题发送.我已经拥有的代码如下,

var header = {
     "access-control-allow-headers":"Content-Type",
     "CLIENT_TOKEN" : "API-TOKEN"
}; 

var options = {
     "method" : "post",
     "header" : header
};

UrlFetchApp.fetch("https://app.rainforestqa.com:443/api/1/runs/TESTNUMBER/tests.json?result=failed", options);
Run Code Online (Sandbox Code Playgroud)

但是这会返回405错误.有没有人有任何想法为什么这不起作用?

谢谢

use*_*253 5

事实证明答案如下,我基本上通过反复试验来解决这个问题.

$var options = {
     "async": true,
     "crossDomain": true,
     "method" : "GET",
     "headers" : {
       "CLIENT_TOKEN" : "my-api-key",
       "cache-control": "no-cache"
     }
   };
var response = UrlFetchApp.fetch("https://app.rainforestqa.com:443/api/1/runs/test_id/tests.json?result=failed", options);
Run Code Online (Sandbox Code Playgroud)

  • 这是设置“method”和“headers”的正确方法,但是选项中包含的“crossDomain”和“async”参数不会执行任何操作。根据 Google 的文档,这些不支持选项:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object) (我的猜测是它们是jQuery 选项虽然......增加了混乱) (2认同)