0

I am trying to access the API of our CRM Documentation through Google Sheets / Apps Script. When accessing the API through Postman I have no issues and get the desired results using the following setup:

POST: https://api.sharpspring.com/pubapi/v1.2/?accountID={{accountID}}&secretKey={{secretKey}}

BODY:

{
  "id":"12345678912345678999",
  "method": "getOpportunities",
  "params": {
        "where": {},
        "limit":"500",
        "offset": "0"
    }
}

Now, when I try to replicate the same in Apps Script I get the following result:

{ result: null, error: { code: 102, message: 'Header missing request ID', data: [] }, id: null }

The function that I am running is as below:

function myFunction() {

  var URL = "https://api.sharpspring.com/pubapi/v1.2/?accountID={{accountID}}&secretKey={{secretKey}}"

  var body = {
    "method": "POST",
    "body": raw,
    "headers": {"Content-Type": "application/json"},
    "redirect": "follow"
  }

  var raw = {
  "method": "getOpportunities",
  "id": "12345678912345678999",
        "params": {
        "where": {},
        "limit":"500",
        "offset": "0"
      }
    }

  var results =  UrlFetchApp.fetch(URL, body).getContentText();
  var data = JSON.parse(results);
  console.log(data);

}

In both I am passing a random session ID "12345678912345678999". I tried finding a session ID in the cookie but that didn't work and I assume that I am on the wrong path there. Passing the id in the header directly didn't work either.

Any ideas? Thanks a lot in advance!

  • Possible related to: https://stackoverflow.com/questions/35133610/google-app-script-urlfetchapp-fetch-with-header – Juan Antonio Feb 03 '21 at 11:55
  • 1
    The order of the code matters. Move the object literal for `raw` above `body`. – Alan Wells Feb 03 '21 at 14:59
  • tried both, thanks. Unfortunately didn't make a difference. Both in different order and moving the id and other options directly into the header didn't work. Still the same error. – HannsProx Feb 03 '21 at 15:26

0 Answers0