Auto Generate Access token for Oauth 2 Outbound Rest Message

Divya2411
Tera Contributor

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.

3 REPLIES 3

Ahmmed Ali
Mega Sage

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

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali
webservice #integration #integrationformula Web Services Integration Day 13 | OAUTH Token - Outbound Integration In this session, I will show you how can we Integrate using OAUTH Token with ServiceNow https://snowexpertrohit.com/child-document/servicenow-to-servicenow-integration/ Contact Us: ...

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.

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.

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali