How to generate token/refresh token Oauth2 automatically ?

Andrei Radules1
Giga Contributor

Hello,

So I am currently integrating 2  servicenow instances and now I am trying to implement the OAuth2 authentification for the outbound and inbound REST messages. 

Currently, I am able to generate the token manually from the REST message. 

My question is if it is possible to automatize the token generation, possibly include it in the business rule, so the script would basically check if the token has expired and if it has it would use the refresh token to generate a new one and use it for authentication?

Many thanks,

Andrei

10 REPLIES 10

Thank you for the advice.

 

How would the schedule workflow look like, what script would i have to include in order to automatize the token generation ?

I am not sure a scheduled job would be the best approach because you could make network calls unnecessarily. If you want to go the "refresh" route then I think your idea of using a business rule would be the best approach, though I think you may potentially run into some "race conditions"/timing issues. Let us know how it works out.

I was able to actually create business rule based on of the tokens are expired or not and if they are it updates the token and refreshes the expiry time.

Hi Andrei,

could you please guide me,

1. How you check if tokens are expired ?

2. How do you refresh the token code ?

 

Thank you

dev

RubenFuertes
Tera Guru

Hi,

Could you refresh the token automatically finally?