Mosh
8 years agoProfessor
Google Apps Script to LogicMonitor REST API
I am trying to use the REST API from Google Apps Script but having trouble getting the auth signature to match the expected values. The code executes but I get HTTP 403 back because the auth signature is not as expected. Here's my Google Apps Script code, hoping someone from LM can point me in the right direction.
I know my accessKey etc. are okay as my PowerShell code works just fine.
Thanks in advance.
var httpVerb = "GET";
var epoch = (new Date).getTime();
var resourcePath = "/device/groups";
var queryParams = "?filter=fullPath~Business Services&fields=id,name";
// Construct signature
var requestVars = httpVerb + epoch + resourcePath;
var message = requestVars;
var secret = accessKey;
var signature = Utilities.computeHmacSha256Signature(message, secret, Utilities.Charset.UTF_8);
var signatureHex = "";
for (var i = 0; i < signature.length; i++)
{
signatureHex += (signature).toString(16);
}
signatureHex = signatureHex.split("-").join("");
signature = Utilities.base64Encode(signatureHex);
Logger.log(signatureHex);
Logger.log(signature);
// Construct HTTP request headers
var auth = "LMv1 " + accessId + ":" + signature + ":" + epoch;
var options =
{
"Authorization" : auth,
"method" : httpVerb,
"contentType" : "application/json"
};
var url = "https://" + accessCompany + ".logicmonitor.com/santaba/rest" + resourcePath + queryParams;
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());