Not able to generate token using existing refresh token
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2023 12:25 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2023 02:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2023 10:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 07:03 AM - edited 04-18-2023 07:03 AM
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.