How to generate token/refresh token Oauth2 automatically ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019 05:22 PM
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
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019 07:21 PM
We had to implement something similar for our client. For us we retrieved the token each time a request was made to the server. This way we did not have to keep track of checking if the token was expired. This was also handy because our clients would leave their desk for extended periods, but leave the application window up so tokens would have expired pretty frequently. This worked for us because we only had a handful of server requests that needed the user token. If you need it more frequently then this approach may not work for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2019 01:16 AM
Thank you for the info!
Could you please let me know what code are you using or how are you integrating it in the business rule to ask for a token everytime ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2019 07:23 AM
Actually, we did not do it in a business rule or script include because we had issues with SN doing the redirects (handling HTTP 302) on the server side. So we did it all on the client side. The browser handled the redirects so that was not an issue. Also, if a reauthentication needs to happen it could be done automatically through the browser. When we retrieved the token we used a GlideAjax to send it to the server and had a ScriptInclude right it to the user's session. When the server needs to make a call it "assumes" the current token in the session is good and uses it for the Authorization when sending the request from SN to the other external services. I hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2019 07:39 PM
try using scheduled workflow.
https://docs.servicenow.com/bundle/madrid-servicenow-platform/page/administer/workflow-administration/reference/r_SchedulingAWorkflow.html