Scripted rest api

Ketan Pandey
Tera Expert

HI,

 

I have created a scripted rest api ,i dont see the option 'Requires authentication' ,there are used to be an option where we can selecte the basic authentication or oauth ,that option is not there in scripted rest api ,is it new change.Please tell how to use the Oauth in my scripted rest api.

4 REPLIES 4

Debasis Pati
Tera Guru

Hello @Ketan Pandey ,

You will find authentication tab in rest messages not in scripted rest api's.
In scripted rest api's you need to send the authentication details in your script only.

More about scripted rest api - https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/integrate/custom-web-services/con...


Please mark it as correct/helpful if this helps you.


Regards,
Debasis

Nilesh Pol
Tera Guru

@Ketan Pandey 

You can use following scirpt to involve Oauth in the Method (GET, POST, etc.) and the Relative Path.

var accessToken = request.headers['Authorization'];

var oauth2Client = new OAuth2Client();

var tokenValidation = oauth2Client.validateAccessToken(accessToken);

var result = {

message: 'OAuth token is valid!',

data: 'Your secured data goes here'

};

response.setStatus(200);

response.setBody(result);

 

Hello @Ketan Pandey ,

Sample script below:

(function getOAuthToken() {
var clientId = 'YOUR_CLIENT_ID';
var clientSecret = 'YOUR_CLIENT_SECRET';
var tokenUrl = 'https://YOUR_INSTANCE.service-now.com/oauth_token.do';

var requestBody = 'grant_type=client_credentials&client_id=' + clientId + '&client_secret=' + clientSecret;
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('POST');
request.setEndpoint(tokenUrl);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setRequestBody(requestBody);

var response = request.execute();
return JSON.parse(response.getBody());
})();

  • Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual OAuth client ID and secret.

  • Replace YOUR_INSTANCE with your ServiceNow instance name.

  • Replace YOUR_TABLE with the table you want to access.



    Please Mark it as correct if this resolves your issue.

    Regards,
    Debasis



 

Kieran Anson
Kilo Patron

This is defined on the rest resource level

KieranAnson_0-1740736609094.png