How to make access token refreshed for every an hour?

Manikantahere
Tera Contributor

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

@akashkumar1 

1 ACCEPTED SOLUTION

@Manikantahere 

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.

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Manikantahere 

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.

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

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.?

 

@Manikantahere 

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.

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

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.?

access-1.png