How to update Refresh token automatically once it expires in oauth2

faizu
Tera Contributor

I have an integration that uses Oauth2 as authorization which uses 2 tokens, Refresh and access token.

Access token are being taken care via code and updated automatically.

In case of refresh token there is no way to update it automatically. 

I provided life time to 1 year for refresh token but the question is how to take care of it once it is about to expire.

Any suggestions to take care of it automatically?

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@faizu 

You can run schedule job which would run Yearly just before the Refresh token expires and regenerate the refresh token

Ideally you can directly consume the API to get the Access token and Refresh token everytime when you hit the actual endpoint

Using this you need not worry on story the access token and refresh token and regenerate them just before expiration

Regards
Ankur

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

Ankur,

by default, access token and refresh token are stored in oauth_credential table. I get both access and refresh token from Rest endpoint however I could not update refresh token via code. Can you please guide?

 

Regards,

Faizan

@faizu 

you can use schedule job to refresh them

script here

Script to retrieve Access and Refresh tokens using GlideOAuthClient libraries

Regards
Ankur

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

Hi Ankur,

Thanks for your response.

I followed below community article and got direction to get new access token and refresh token for each request.

https://community.servicenow.com/community?id=community_blog&sys_id=d547f492db61d300fc5b7a9e0f9619d2

however I am using only access token and it is working.

there are 2 tokens, Refresh and Access. I beleive we need to send access token with rest call for authorization.

What is the use of refresh token?

In my code I did not use reresh token. is it ok?

Regards,

Faizan