Search code examples
google-apps-scriptgoogle-sheetsapi-key

How to provide an API key for ssl-vp.com in a Google Sheets script?


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());
}

Solution

  • 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
      }
    };