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

Bindu22
Tera Contributor

Hi Aman,

 

Unable to decrypt the refresh token using this code. It is printing null.

Have tried token_received.getDecryptedValue() also but that is printing undefined.

 

Is there any other way?

VTPhil
Tera Contributor

This is a perfect example. Thanks!

leoferrero
Tera Contributor

Hi! I have a similar issue with Salesforce integration User Not Authenticated. Could not retrieve a new access token with the refresh token. invalid_request, Missing parameters: access_token

 

Can anyone help me

 

Thanks

Mathieu Lepoutr
Mega Guru

Hi Sundars

 

It seems that you're facing an issue with refreshing the access token when it expires in your ServiceNow integration with an external system using OAuth 2.0 authentication. It's important to ensure that you obtain a new access token before making any subsequent API calls.

 

 

Please try these suggestions:

  1. Verify OAuth Configuration: Double-check your OAuth 2.0 application registry settings in ServiceNow.

  2. Review Token Expiration: Confirm the expiration time of the access token received from the token response. You can use token.getExpiresIn() to check the token's expiration time. If the token is already expired or close to expiration, you'll need to refresh it.

  3. Implement Token Refresh Logic: Implement a token refresh mechanism to obtain a new access token when the current one expires. This usually involves sending a refresh token to the token endpoint in exchange for a new access token. Make sure you handle token refresh logic securely and follow the OAuth 2.0 specification.

  4. Check Error Logs: Examine the error logs or debug output for any specific error messages or exceptions that might provide additional insights into the issue. The gs.print() statements in your code can help you identify any potential errors.

  5. Test OAuth Flow: You mentioned that when you click on "Get OAuth Token" and "Test" in the ServiceNow interface, it works fine. Consider retesting the OAuth flow to ensure that the token retrieval and API calls are functioning as expected outside the context of the business rule.

Considering the complexity involved in troubleshooting and configuring OAuth integrations, it might be worth exploring a specialized integration tool like Exalate.  I have been using it for quite a long time and it works perfect for use-cases like this.