Access RESTApi from inside ServiceNow Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 04:35 AM
Hello,
I am trying to access CMDB using ServiceNow given RESTApi from Script include.
Password i can read of current user using below code
+++++++++++++++++++++++++++
var usr = new GlideRecord('sys_user');
var tmp = usr.get(gs.getUserID());
gs.info("Password outside: {0}", usr.user_password);
+++++++++++++++++++++++++++
But its 1 way Encrypted password so we can't decrypted . and when we use in REST APi call
+++++++++++++++++++++++++++++++
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<InstanceName>.service-now.com/api/now/table/<tableName>');
request.setHttpMethod('POST');
var user = gs.getUserName();
var password = usr.user_password;
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody(<JSON>);
var response = request.execute();
+++++++++++++++++++++++++++++++
Is there any way to authenticate current user and get password?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 04:46 AM
Hi Ankit,
What is your requirement here?
You are accessing table within ServiceNow via REST Table API call rather than querying that.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 04:50 AM
yes, we are access REST API from Script Include inside ServiceNow not from outside. But need to pass user / password. how to pass that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 05:01 AM
Hi Ankit,
Since password field on user table is one way encrypted; you cannot decrypt it and hence cannot use the value in API.
Why not go with system property and store username and password in it and fetch that in script include and use those.
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2018 05:07 AM
But I was thinking if i am running script include on same ServiceNow instance and with same user. So i should be able to use that user and access RESTApi. because this is not from outside ServiceNow.