如何在Google表格脚本中为ssl-vp.com提供API密钥?

问题描述 投票:0回答:1

我正在尝试从URL获取JSON数据并将数据插入Google表格的表格中。问题是当我尝试获取数据时,该错误提示我:

请求返回https://ssl-vp.com/rest/v1/Campaigns/1236223656534991/Recipients?by=ExternalId&page=1&itemsPerPage=500的代码401时出错。截断的服务器响应:未经授权无法获取活动联系人(使用mutantHttpExceptions选项检查整个响应)(第15行,文件“Código”)

我尝试在URL中插入API KEY,但响应是相同的错误

function myFunction(){
var url = "https://ssl-vp.com/rest/v1/Campaigns/1236223656534991/Recipients?by=ExternalId&page=1&itemsPerPage=500";
var apiKey = "xxxxxxxxxxxxxxxxxxxxx";
  var header={
    "headers":{
         "X-API-KEY":apiKey
    }
  };
var response = UrlFetchApp.fetch(url,header);
  Logger.log(response.getContentText());
}
google-apps-script google-sheets api-key
1个回答
2
投票

根据ssl-vp.com's API docs,此服务需要Authorization标头中的API密钥,如下所示:

Authorization: Bearer <API_KEY>

将代码更改为:

var header = {
  "headers": {
    "Authorization": "Bearer " + apiKey
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.