Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Calling API REST through action script from once instance to another - Configured Oauth 2.0

Bhuvi1
Tera Contributor

Unable to call other instance REST API(script) from action script and Oauth is working fine only if the request is on same instance.But when i tried to call other instance, getting 'User Not Authenticated'. Please somebody help on this. Thanks in advance

below is my script..

---------

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + item.variables.select_instance.u_name + '.service-now.com/api/

 

/mfa_reset);
request.setHttpMethod('PATCH');

 

var accesstoken = GetOauthAccessToken();
request.setAuthenticationProfile('OAuth2', 'd9fd02b9874ce194340532a73cbb35e3');
request.setRequestHeader('Authorization', 'Bearer ' + accesstoken);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setEccParameter("skip_sensor", "true");

var response = request.execute();
gs.log('Response from scripted API1 = ' + response.getBody());
var responseObject = JSON.parse(response.getBody());
outputs['result'] = responseObject.result.message;

// Getting accesstoken form Oauth
function GetOauthAccessToken() {
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {
'grant_type': 'password',
'client_id': '88b7dea5e8486910c465e75ba4dfd823',
'client_secret': ']KZom1*bvH',
'username': 'user_auth,
'password': 'user_auth'
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('platformAutomation_oauth_config', text);
var token = tokenResponse.getToken();

gs.log("AccessToken:" + token.getAccessToken());
//gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
//gs.log("AccessToken RefreshToken:" + token.getRefreshToken());
return token.getAccessToken();
}

-------

2 REPLIES 2

jaheerhattiwale
Mega Sage

@Bhuvi1 While creating oauth profile you must specify the target instance url in oauth_entity table?

 

jaheerhattiwale_0-1672322748858.png

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Bhuvi1_0-1672336007061.png

yes i did mentioned in oauth scope field while creating oauth profile in target instance. is it correct?

and 

request.setAuthenticationProfile('OAuth2', 'd9fd02b9874ce194340532a73cbb35e3');
here the above 'd9fd02b9874ce194340532a73cbb35e3' is sys_id of oauth profile