The CreatorCon Call for Content is officially open! Get started here.

Script to use for REST API Call from one instance to another when using OAuth 2.0

quiksilver
Mega Guru

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

find_real_file.png

 

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);

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I have blog around this documented; that may help you

It mentions how OAuth can be configured

OAuth 2.0 with Inbound REST

For knowing how to consume the REST Message using OAuth refer below links

https://docs.servicenow.com/bundle/orlando-application-development/page/integrate/outbound-rest/concept/c_OAuth2ProfileTutorialGoogle.html

https://community.servicenow.com/community?id=community_question&sys_id=75f74f2ddb1cdbc01dcaf3231f961944

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

Hello Ankur

THanks for your reply

When I run this code via 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('May28default_profile', text);

var token = tokenResponse.getToken();

gs.log("AccessToken:" + token.getAccessToken());

gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());

gs.log(" RefreshToken:" + token.getRefreshToken());

 

I get this 

 

failed to find the oauth entity.
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null

 

I used this profile, is grant type correct? 

 

find_real_file.png

Harsh Vardhan
Giga Patron

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

Hello harshvardhan,

 

Do you know what should be the value of grant type 

This is my application registry that I use to connect to another snow instance

 

find_real_file.png

 

when I run this 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('May28default_profile', text);

var token = tokenResponse.getToken();

gs.log("AccessToken:" + token.getAccessToken());

gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());

gs.log(" RefreshToken:" + token.getRefreshToken());

 

I get this 

 

failed to find the oauth entity.
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null