问题
I'm trying to get JSON data from a URL and insert the data in the sheets of Google Sheets. The problem is when I try to get the data this response me that error:
Error in the request for the returned code 401 of https://ssl-vp.com/rest/v1/Campaigns/1236223656534991/Recipients?by=ExternalId&page=1&itemsPerPage=500. Truncated server response: Unauthorized Cannot get campaign contacts (use the muteHttpExceptions option to examine the entire response) (line 15, file "Código")
I try to insert the API KEY in the URL but the response is the same error
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());
}
回答1:
According to ssl-vp.com's API docs, this service expects the API key in the Authorization
header like so:
Authorization: Bearer <API_KEY>
Change your code to:
var header = {
"headers": {
"Authorization": "Bearer " + apiKey
}
};
来源:https://stackoverflow.com/questions/57003438/how-to-provide-an-api-key-for-ssl-vp-com-in-a-google-sheets-script