Not able to generate token using existing refresh token

Nivedita9
Tera Contributor

Hi @Ankur Bawiskar 
I need your suggestion here.
We are integrating with SAP Ariba using oAuth 2.0 )grant type - client credential).
I tested my rest message it give me 200 response. After placing the logic in script include I found out that access token is not getting generated using existing refresh token.

Code which I'm using:

/*This is the only information you need before hand*/
var Application_Registry_Name = 'My application registry';

/*Attempt to find the existing Refresh Token*/
var refreshToken = '';
var grOC = new GlideRecord('oauth_credential');
grOC.addEncodedQuery('type=refresh_token^expiresRELATIVEGT@minute@ahead@1^peer.name='+Application_Registry_Name);
grOC.query();
if(grOC.next()){
var Encrypter = new GlideEncrypter();
refreshToken = Encrypter.decrypt(grOC.token_received);
}

/*If a valid Refresh Token Exists, then request the new Access Token*/
if(JSUtil.notNil(refreshToken)){
//GlideOAuthClientRequest
var clientRequest = new sn_auth.GlideOAuthClientRequest();
clientRequest.setGrantType('refresh_token');
clientRequest.setRefreshToken(refreshToken);
//GlideOAuthClient
var client = new sn_auth.GlideOAuthClient();
//GlideOAuthClientResponse
var tokenResponse = client.requestTokenByRequest(Application_Registry_Name , clientRequest);
//GlideOAuthToken
var token = tokenResponse.getToken();

gs.info('Existing Refresh Token: '+refreshToken);
gs.info("New Access Token:" + token.getAccessToken());
gs.info("New Access Token Expires In:" + token.getExpiresIn()+' Seconds');

}else{
gs.info('Unable to find an existing refresh token');
}

 

 

Error I'm getting:

Nivedita9_0-1679729117265.png

 

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Nivedita9 ,

 

Please ensure that the refreshToken variable is not empty or null before passing it to the setRefreshToken() function. You can add a check to confirm that the refreshToken is not empty or null, as follows:

 

if(JSUtil.notNil(refreshToken)){
   //GlideOAuthClientRequest
   var clientRequest = new sn_auth.GlideOAuthClientRequest();
   clientRequest.setGrantType('refresh_token');
   if(JSUtil.notNil(refreshToken)) {
       clientRequest.setRefreshToken(refreshToken);
   }
   //... rest of the code
}

 

 

If the issue persists, please ensure that the oauth_credential record exists in the ServiceNow instance with the correct details for the SAP Ariba integration. You can check the oauth_credential table to confirm if the record exists and if it has the correct values.

 

If my response helps you to resolve the issue close the question by Accepting solution and hit 👍thumb icon. From Correct answers others will get benefited in future.

 

Thanks,

Ratnakar

Hi @Ratnakar7 
Thanks a lot for responding on weekend. I checked the refresh token is being passed correctly/
About OAuth- The oAuth profile is correct because I'm able to test it manually by click on Get OAuth Token link on rest message.

Divya2411
Tera Contributor

Hi @Nivedita9 

For me also same problem, not able to generate access token using refresh token. Did you got any solution?

 

if then kindly help.