Thursday, September 29, 2022

ServiceNow script to retrieve oAuth token programatically

var instance = 'https://test.service-now.com';

var username = 'testadmin1'; 

var password = 'testadmin1'; 

var clientId = '539decb2e5968b00f5ac882d078e787d'; 

var clientSecret = 'test123'; 

  

// Request a token 

var getTokenRM = new sn_ws.RESTMessageV2(); 

getTokenRM.setEndpoint(instance + '/oauth_token.do'); 

getTokenRM.setHttpMethod("post"); 

getTokenRM.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 

getTokenRM.setRequestBody('grant_type=password&client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&password=' + password); 

var responseToken = getTokenRM.execute(); 

gs.print('response..'+ responseToken.getBody());

var token = new JSONParser().parse(responseToken.getBody()); 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.