Skip to content

Instantly share code, notes, and snippets.

@ArisBee
Created March 2, 2018 15:01
Show Gist options
  • Save ArisBee/683b8bbf0bc4680b078f815b1b81c05c to your computer and use it in GitHub Desktop.
Save ArisBee/683b8bbf0bc4680b078f815b1b81c05c to your computer and use it in GitHub Desktop.
Simple password ServiceNow API authentication in Google script.
function SNOWsimpleAuth() {
// Those are environnement variables defined in File>Project properties>Script properties
var username = PropertiesService.getScriptProperties().getProperty('LOGIN');
var password = PropertiesService.getScriptProperties().getProperty('PASSWORD');
// I will use Google script UrlFetchApp class to make request, which is more powerfull than XMLHttpRequest
var headers = {
'Authorization': 'Basic ' + Utilities.base64Encode(username + ":" + password)
};
var options = {
'method' : 'get',
'accept': 'application/json',
'headers':headers
};
try{
// Let's query the first incident of a SNOW testing developper instance
var response = UrlFetchApp.fetch("https://devXXXXX.service-now.com/api/now/table/incident?sysparm_limit=1", options);
}catch(e){
Logger.log("caught this:" + e);
}
Logger.log("Before JSON parsed "+ response.getContentText());
var responseBody = JSON.parse(response.getContentText())['result'];
Logger.log(responseBody.result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment