Retrive Oauth Token from script

alebuc
Tera Expert

Hi community,

 

I am trying to get the access token from script as follows:

 

var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://<INSTANCE NAME>.service-now.com/oauth_token.do');
r.setHttpMethod('POST');

r.setRequestHeader('content-type', 'application/x-www-form-urlencoded');

r.setRequestBody('grant_type=password&username=ABC&password=123&client_id=xxxxx&client_secret=yyyyyy');

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

//***Return Response and httpStatus
var res = {};
res.httpStatus = httpStatus.toString();
res.responseBody = JSON.parse(responseBody);
 
This is the result:
*** Script: {
  "httpStatus": "401",
  "responseBody": {
    "error_description": "access_denied",
    "error": "server_error"
  }
}

 

From Postman it works just fine.

 

Can someone help me?

 

Thanks

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@alebuc 

error 401 means incorrect username/password

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

From Postman with the same values it works fine. The format of the body is correct?
Thanks