OAuth token lasts for 30 minutes

prakhar_yadav
Tera Contributor

We need to manually click on "Get Oauth token" in related links after every 30 mins as it last for that much time only. Is there a way so that, this process can be automated, if YES, please help by guiding step by step. Thanks in advance

7 REPLIES 7

Hi @prakhar_yadav 

 

I am not much sure but you will need to do manual intervention to get OAuth token. Would recommend you to use Client Credentials. I have done with client credentials. You can change the grant type and use the following code to get the access_token seamlessly-

 

var client_id = 'Enter your client id';
        var client_secret = 'Enter your client secret';
        var grant_type = 'client_credentials';
        var token_endpoint = 'Enter your endpoint';
        var token_grant_url = token_endpoint + "?grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;

        var token_request = new sn_ws.RESTMessageV2();
        token_request.setEndpoint(token_grant_url);
        token_request.setHttpMethod("post");

        var token_response = token_request.execute();
        var response_body = token_response.getBody();
        var response_obj = JSON.parse(response_body);
        var access_token = response_obj.access_token;

        gs.info('Token request response: ' + response_body);
        gs.info('Access token: ' + access_token);

 

Please mark my answer helpful and correct.

 

Regards,

Amit

Hi @Amit Pandey how can I pass the token with request payload? Your help is highly appreciated 

Hi @ServN 

 

Please check my reply -

 

https://www.servicenow.com/community/hrsd-forum/i-have-to-do-integration-from-servicenow-to-third-pa...

 

Kindly mark my answer helpful and correct.

 

Regards,

Amit