Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

While Generating tocken Getting { "error_description":"access_denied","error":"server_error}

Gillerla Rajesh
Tera Contributor
script written to generate token
----------------------------------------------
var clientid = 'f765863133c4421f9e5404cff424d6a9';
var clientsecret ='qga<;P(y+eA8zeL~<O*b+&^,g68f`^cB';
var username = "admin";
var password = "kkkkkkkkkkkkkk";
var atoken = '';
var rtoken = '';

function getToken() {
    var token = new sn_ws.RESTMessageV2();
    token.setHttpMethod('POST');
    token.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    token.setRequestBody("grant_type=password&username="+username+"&password="+password +"&client_secret=" + clientsecret + "&client_id=" + clientid);
    var result = token.execute();
    var statuscode = result.getStatusCode();
    var body = result.getBody();
    return statuscode +"  "+body;
}
gs.info(getToken());
 
--------------------
Error 
-------------------
No URIs provided, removing all URI matcher rules
*** Script: 401 {"error_description":"access_denied","error":"server_error"}
 in Postman it's working but with script not working exactly.

 

 
GillerlaRajesh_0-1762518826629.png
 
 
 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Gillerla Rajesh 

401 means credential issue.

If it's working from postman then you are good, why you are concerned about the error while trying to get token within the instance?

try to update this line

token.setRequestHeader("content-type", "application/x-www-form-urlencoded");

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 
yes your correct , but when i try with script to generate token means at the time its giving error. not sure where i did mistake

 

Regards,

Rajesh Gillerla.