Auto Generate Access token for Oauth 2 Outbound Rest Message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2023 12:33 AM
I want to connect servicenow with third party integration through Api, but my access token expires everytime. So i want to auto generate the access token everytime its expired. For that need Script code, how to generate it automatically.
I Tried some code but getting the error, "Bearer token is missing in the HTTP request authorization header".
Kindly Help.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2023 05:36 AM
You can configure "Application registry" and use authentication as that record in your REST API record. ServiceNow will automatically manage access and refresh tokens.
below videos shows one example:
https://www.youtube.com/watch?v=4uAXcgwjNVQ
https://www.youtube.com/watch?v=QRjjZ911bZE
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2023 07:08 AM
yes, i configure application registry and rest outbound message but if the token expire i need to generate it again manually. I want the script for auto generation of the access token , i tried some code from community but it's not working:-
doIt();
function doIt() {
var sm = new sn_ws.RESTMessageV2();
sm.setEndpoint("oauth token endpoint");
sm.setHttpMethod("post");
sm.setRequestHeader("Accept", "Application/json");
sm.setQueryParameter("grant_type", "client_credentials");
sm.setQueryParameter("client_id", "enter the client_id here");
sm.setQueryParameter("client_secret", "enter the client_secret here");
var response = sm.execute();
var responseBody = response.getBody();
gs.print(responseBody);
//Fetch Access Token from Response Body
var resp = new JSONParser().parse(responseBody);
gs.print('Access Token: ' + resp.access_token);
}
It showing the errors.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-06-2023 07:20 AM
What is the lifespan of refresh token? If access token is expired, new token can be generated using access token. You need to keep the refresh token lifespan more value.
Thank you,
Ali