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

@Manikantahere 

since you want access token using client_credentials you will have to use username and password as well

check this link and response from Ram

OAuth : Script to Automate Token Request 

sharing here as well

var req = new sn_ws.RESTMessageV2();
req.setEndPoint( < OAUTh end point URL >);
req.setHttpMethod('POST');
req.setRequestHeader('content-type','application/x-www-form-urlencoded') 
req.setRequestBody(grant_type=" + grant_type + "&username=" + username + "&password="+encodeURI(pwd)+ "&client_id="+client_id+"+ "&client_secret="+client_secret));
var response = req.ExecuteAsync();
response.WaitForResponse(10);
var responseBody = response.getBody();
var obj = JSON.parse(responseBody);
gs.info('Token : "+obj.access_token);

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

@Manikantahere 

Thank you for marking my response as helpful.

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

This is not the way that I want to do Ankur

I have rest message created for it  and OAuth Provider and Profile attached to Rest message.

either with the rest message or with the Provider or Profile I need to get the access token.??

Note: When I click on Rest Message Get OAuth Token I am able to see flow was successful and access token is populated in Oauth_credentials table.

@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