- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 04:31 PM
Hi All,
I have a working script shown below that connects one service now instance from another via REST API. It is running okay with using basic authentication, but when I try to use OAuth 2.0, that is where the issue is. Can anyone point out my mistake please ?
I tried using this
sm.setAuthenticationProfile('oauth2', '1c79c7e12f8dd8108839d5ea2799b6c3');
where sysid "1c79c7e12f8dd8108839d5ea2799b6c3" is pointing to this
Thanks
** here is the code I am running in background script
var requestBody;
var responseBody;
var status;
var sm;
try{
//May28REST is the name of my oubound REST message
//get_tickets is the name of http method under May28REST
sm = new sn_ws.RESTMessageV2("May28REST", "get_tickets");
sm.setBasicAuth("admin","admin");
//set auth profile to an OAuth 2.0 profile record.
// sm.setAuthenticationProfile('oauth2', '1c79c7e12f8dd8108839d5ea2799b6c3');
sm.setQueryParameter("sysparm_query","active=false^number=INCV0135986");
sm.setRequestHeader("Accept","Application/json");
response = sm.execute();
responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();
status = response.getStatusCode();
} catch(ex) {
responseBody = ex.getMessage();
status = '500';
} finally {
requestBody = sm ? sm.getRequestBody():null;
}
gs.log('here here here : '+responseBody);
Solved! Go to Solution.
- Labels:
-
Platform and Cloud Security

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2020 12:45 AM
it has generated token ? when you run the code in background script are you getting any error ?
kindly have a look on below blog ?
How to setup OAuth2 authentication for RESTMessageV2 integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2020 10:17 PM
where are you running this script ?
Just confirming, are you connecting this between two instance ? if yes then you have to build oauth in first instance 1 "Create an OAuth API endpoint for external clients" and then you will create another oauth in instance2 "Connect to a third party OAuth Provider" and then you will run the above code in instance 2.
also in below line, you will provide instance 1 id and password.
var params = {grant_type:"password", username:'uname', password:'pw'};
it will work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2020 10:29 PM
Hi
I am running this via Background script.
Yes I am filling that line with the proper username and password.
I am connecting 2 service now instance and the instance 1 has application registry set up. I am able to connect to instance 1 via postman.
But having trouble using the script from another service now instance
Thanks
Quik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2020 10:39 PM
is it possible if you can connect me over zoom or google hangout and share you screen, this way i can quickly help you. if yes drop me an email
hvrdhn88@gmail.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2020 11:14 PM
It Worked.
I just had to change this line
var tokenResponse = oAuthClient.requestToken('May28', text);
where May28 is the name of the Application Registry (oauth_entity)
(I was using Application Registry Profile before)
-- Working code from background script
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {grant_type:"password", username:'uname', password:'pw'};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('May28', text);
var token = tokenResponse.getToken();
gs.log("AccessToken:" + token.getAccessToken());
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2020 12:48 AM
HI,
Check this out:
https://www.youtube.com/watch?v=j-r3yEDLpKc
THanks,
Ashutosh