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

How to generate the access token once it is expired from Business Rule?

sundars
Kilo Contributor

Dear All, 

Please need urgent help.

I working on integration from ServiceNow to an external system with OAuth 2.0 authentication. 

Step 1:- I have set up the OAuth2.0 Application Registry for the third party with the Clint ID and secrete Id   

Step 2:- I have set up the REST Message using this OAuth2 default profile and set up the post method.

When I click on "Get OAuth Token" it authenticated successfully and when I clicked on the "Test" it gave me a success message.

Step 3 (Not working step):- I have set up "Business Rule" to call the REST Message this works but when the Access Token expired and when I try it is not working. so in the script itself, I am trying to get new access toke but it is not working. Can anyone please help me on this what I am doing wrong.

(function executeRule(current, previous /*null when async*/) {

try { 

var oAuthClient = new GlideOAuthClient();
var params ={grant_type:"password", username:"admin@externalsystem.com", password:"pass@123"};
var json =new JSON();
var text = JSON.stringify(params);
gs.print("text==:" + text);
var tokenResponse = oAuthClient.requestToken('xxxx default_profile', text);
gs.print("tokenResponse==:" + tokenResponse);
var token = tokenResponse.getToken();
var accessToken = token.getAccessToken();
var RefreshToken = token.getRefreshToken();
gs.print("token==:" + token);
gs.print("AccessTokenExpiresIn:"+ token.getExpiresIn());
gs.print("accessToken==:" + accessToken);
gs.print("RefreshToken==:" + RefreshToken);


var r = new sn_ws.RESTMessageV2('Case Create API', 'post');
r.setRequestHeader("Content-Type","application/json");
r.setRequestHeader("Authorization", "Bearer " + accessToken);
r.setAuthentication("oauth2", "xxxx default_profile");


r.setStringParameter("sysid",current.sys_id);
r.setStringParameter("number",current.number);
r.setStringParameter("description",current.description);
r.setStringParameter("priority",current.priority);
	
 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
 gs.print("ex==:" + ex);
}


})(current, previous);
8 REPLIES 8

Ct111
Tera Sage

Hi,

 

This is  a script which i found for token refresh in google API

 

https://developers.google.com/google-ads/scripts/docs/examples/generate-oauth20-refresh-token

 

 

Check this if it helps you in modifying ur code accordingly or not

 

Mark my ANSWER as CORRECT and HELPFUL if it helped

ARG645
Tera Guru

Code snippet to get a new Access Token when the previous Access token expires.

1. You need the Refresh toke to execute this code

2. You need the OAuth Applciation registry Name to get the new Access Token.

//GlideOAuthClientRequest
var clientRequest = new sn_auth.GlideOAuthClientRequest();
clientRequest.setGrantType('refresh_token');
clientRequest.setRefreshToken('INSERT_REFRESH_TOKEN');
//GlideOAuthClient
var client = new sn_auth.GlideOAuthClient();
//GlideOAuthClientResponse
var tokenResponse = client.requestTokenByRequest('INSERT_APPLICATION_REGISTRY_NAME', clientRequest);
//GlideOAuthToken
var token = tokenResponse.getToken();
gs.debug("Access Token: " + token.getAccessToken());

I set oAuth registry like this, my business rules, script include everything is working fine. but when I open REST request it says OAuth token expired.client given token expire time as 60 min, do I need to set 3600 in Refresh token lifespan? I would like to know how to set this token to auto-refresh? 

Please help me..

 

find_real_file.png

Did you get an answer for this?