How to obtain OAuth new Refresh token once it get's expired

chanikya
Mega Sage

Hi All,

Kindly help me , how to Auto get Refresh token once it's get expired. and how to get's updated under oauth_credential_list.do Table.
Here OAuth 2.0 Refresh token got expired. 

chanikya_0-1764127661164.png

Kindly let me know how to get renew refresh token.

BackGroud script side I tried below part , it is working successfully. But not Sure where to apply and how to get refresh token  , get's updated under oauth_credential_list.do Table.

var tokenRequest = new GlideOAuthClientRequest();
tokenRequest.setGrantType("password");
tokenRequest.setUserName("DEV206450");
tokenRequest.setPassword("f67[Fm>P0[e");
tokenRequest.setScope(null);

var oAuthClient = new GlideOAuthClient();
var tokenResponse = oAuthClient.requestToken("DEV206450", tokenRequest);
gs.log("Error:" + tokenResponse.getErrorMessage());

var token = tokenResponse.getToken();
if (token) {
    gs.log("AccessToken:" + token.getAccessToken());
    gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
    gs.log("RefreshToken:" + token.getRefreshToken());
}

*** Script: Error:null
*** Script: AccessToken:G_mDquRyaFsk8uJx2FpbPISZ1xB2sAKFmxJNsU4ulgBxP9-tkL077GXsiBvDbUwGK4iPM1IRxhdALEu40E0uvQ
*** Script: AccessTokenExpiresIn:299
*** Script: RefreshToken:OMYeXOqcqDNenLEORuXEqr4xKz2yIoZ2s5v0IkeCwhSVGVxRoayi4siC48ZtY__ehsVHSAJR0g6Hl1CPTHBK6Q




 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@chanikya 

it's always best to generate and use your above script everytime you are making the actual API call

-> your above script gets the access token

-> use that in your next API call

With this you need not worry on generating token when refresh token expires

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@chanikya 

so you are consuming OAuth endpoint of another DEV instance?

what's the grant type for that in target instance?

share where are you creating the above script and how you are consuming the target API via REST Message in script or flow

Share detailed steps with screenshots.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

Yes, I'm consuming  Instance 1 OAuth details in Instance 2 OAuth Profile.

I put life span of Refresh token as 8mins, Access token 5 min. accordingly succesfuly Tokens created in Instance2 auth_credential_list.do Table.

After 8mins Refresh token gets expires , of course in Instance2 we can click on  "
Get OAuth Token" related link in RESt message to get's created new Refresh token in Instance2 auth_credential_list.do Table.

but I would like to automate this when expire date /time comes one-day before  itself only new Tokens has to be created in Instance2 auth_credential_list.do Table.

Not sure how to design this behaviour to gets created in Instance2 auth_credential_list.do Table. So API's will not face any issue.

Can you please advise me some solution how to get new tokens Instance2 auth_credential_list.do Table. before expire date /time reach


here I'm sharing screenshot 

chanikya_0-1764139187915.png

 

 

@chanikya 

the instance which is OAuth provider where you create OAuth registry -> there grant type is what value?

why not use the same above script everytime you make an API call then you need not worry on expiration?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader