Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Outbound REST Message and client credentials

Sanjay Rallapal
Tera Expert

Hello all

I need to invoke a REST Message which supports client credentials as the Oauth grant type. I followed the docs and I have defined a Oauth profile. I have also verified that SNOW is able to obtain an access token but this happens only when I request for an access token manually.

 

For this integration of work at runtime, SNOW needs to be able to obtain the access token using client credentials without human intervention.  I have looked at prior solutions where folks have suggested writing a job
https://www.servicenow.com/community/developer-forum/access-token-in-case-of-client-credentials-gran...


or have referred to the following Support Note https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0791131

Am I correct in assuming that there is no way that ServiceNow will automatically obtain the access token?

1 ACCEPTED SOLUTION

Omkar Mone
Mega Sage

Why not generate the token by saving it as a REST Message and then invoking it using the RestMessage API? Here's an example:

  1. Save the REST Message:
    Configure a REST Message in ServiceNow with the necessary endpoint and authentication details.

  2. Call it with the RestMessage API:

var restMessage = new sn_ws.RESTMessageV2('MyTokenEndpoint', 'Default');
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info('Response Status: ' + httpStatus);
gs.info('Response Body: ' + responseBody);

 

In this example, the MyTokenEndpoint is the name of the saved REST Message, and you can use the RestMessageV2 API to invoke it directly.

View solution in original post

2 REPLIES 2

Omkar Mone
Mega Sage

Why not generate the token by saving it as a REST Message and then invoking it using the RestMessage API? Here's an example:

  1. Save the REST Message:
    Configure a REST Message in ServiceNow with the necessary endpoint and authentication details.

  2. Call it with the RestMessage API:

var restMessage = new sn_ws.RESTMessageV2('MyTokenEndpoint', 'Default');
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info('Response Status: ' + httpStatus);
gs.info('Response Body: ' + responseBody);

 

In this example, the MyTokenEndpoint is the name of the saved REST Message, and you can use the RestMessageV2 API to invoke it directly.

Thank you @Omkar Mone 
That approach worked. 

I defined the REST Message using System Web Services --> Outbound REST Message. The client credentials flow, in my scenario, uses POST so I defined that including the scopes.
Then I followed your steps in my script with one change. To get the access token, I used JSON.parse

var access_token = JSON.parse(response.getBody()).access_token