Bearer token for REST Integration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-11-2024 08:19 AM - edited ‎05-11-2024 08:20 AM
I'm trying to get the list of organization codes via API, however using the script included below, I receive the error message 'GetCostCenterDetails:getORG:Error: undefined: no thrown error'.
What could have gone wrong?
var GetCostCenterDetails = Class.create();
GetCostCenterDetails.prototype = {
initialize: function() {},
getToken: function() {
try {
var rest = new sn_ws.RESTMessageV2('CostCenterCode', 'Get Token');
rest.setLogLevel('all');
var response = rest.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus.toString().startsWith("2")) {
var parsedData = JSON.parse(responseBody);
var token = parsedData.access_token;
return token;
} else {
gs.error("Error occured ");
}
} catch (ex) {
var message = ex.getMessage();
gs.error("Token:Error: " + message);
}
},
getORG: function() {
try {
var result = {};
var fund = 'GF'; //testing purpose
var rest1 = new sn_ws.RESTMessageV2('CostCenterCode', "Get ORG");
rest1.setQueryParameter('p_fund', fund);
rest1.setRequestHeader('Authorization', 'Bearer ' + token);
var response1 = rest1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
if (httpStatus1.toString().startsWith("2")) {
var parsedData1 = JSON.parse(responseBody1);
if (parsedData1.value) {
fields = [];
var keys = Object.keys(parsedData1.value[0]);
for (var i = 0; i < keys.length; i++) {
var resultObject = {};
resultObject.name = keys[i];
resultObject.display_value = parsedData1.value[0][keys[i]];
fields.push(resultObject);
}
result.fields = [];
result.fields = fields;
}
} else {
gs.error('Error occured 2');
}
} catch (ex) {
var message = ex.getMessage();
gs.error("GetCostCenterDetails:getORG:Error: " + message);
}
return result;
},
type: 'GetCostCenterDetails'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-11-2024 09:44 AM
For your getORG function you're not passing in 'token' explicitly, so you could easily run into an undefined variable error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 05:44 AM
Thanks! How do I pass the token?
I tried the two methods listed below, but to no avail.
----------------------1st Trial-----------------------------------------------------------
getORG: function() {
try {
var result = {};
var token1 = this.getToken(); //added here
var fund = 'GF'; //testing purpose
var rest1 = new sn_ws.RESTMessageV2('CostCenterCode', "Get ORG");
rest1.setQueryParameter('p_fund', fund);
rest1.setRequestHeader('Authorization', 'Bearer ' + token);
----------------------2nd Trial-----------------------------------------------------------
rest1.setRequestHeader('Authorization', 'Bearer ' + this.getToken());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2024 09:27 AM
Rather than trying to do it all by script, create an application registry that will perform the Bearer Token retrieval process for you. https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/administer/notification/task/regis...
Then, on the scripted API resource, you can specify the authentication type and link it to the application registry. That way you don't need any scripted logic in your script include to handle tokens.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2024 09:09 AM
Thank you for the link. This is too technical for my understanding. Would you mind sharing the steps so that I can follow?