- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 11:29 PM - edited 02-17-2025 11:30 PM
If I am calling the Rest Message from the Script I need to explicitly pass the access token as a authorization header but the life span of it is 1hr.
Do I need to write Schedule job or as I attached OAuth Profile it will automatically refresh it in ServiceNow???
Note: Its not refreshing for me am I missing any configuration?
@Ankur Bawiskar
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 04:17 AM
that's what I am saying.
You can use the script to generate the access token before making the actual API call where you require the token to be passed in header
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 11:32 PM
you can get the access token every time just before your actual API call
You can use script to obtain the refresh and access token from script
refer below links
OAuth2.0: Get new Access Token from existing Refresh Token
How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations
OAuth : Script to Automate Token Request
How to generate the access token once it is expired from Business Rule?
How to get Auth Token using script when grant type is Authorization code
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 02:56 AM
I am actually getting access token when I click on Get OAuth Token from Rest Message however as I always call Rest Message from the script I mandatorily need to pass access token as Authorization Header.
I am not able to utilize the above mentioned articles to use token inside my script.
Note: I kept Grant Type as Client Credentials in OAuth Profile that I attached to Rest Message.
One I need to fetch the access token to pass it as a authorization and one more struggle I am facing is I need to get the new access token before its life span ends.?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 03:33 AM
The links I shared will help you getting access token.
Use that just before calling your actual REST Message and use that token in header
With this you need not worry about the lifespan of access token as you are getting it every time.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 04:06 AM - edited 02-18-2025 04:06 AM
Thanks for your response Ankur!!
The below script I tried from Background Script it is returning null saying failed to find the oauth entity.
In request token I tried with both the names and sys'Ids of Oauth provider and Oauth entity profiles.?