Calling API REST through action script from once instance to another - Configured Oauth 2.0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2022 05:19 AM - edited 12-29-2022 09:49 AM
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();
}
-------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2022 06:06 AM
@Bhuvi1 While creating oauth profile you must specify the target instance url in oauth_entity table?
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2022 09:46 AM - edited 12-29-2022 09:47 AM
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