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.

Unable to use HttpRequestAuthedData

Vishal78
Tera Contributor

The documentation at the below url, identifies the 5 steps listed below:

 

https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...

 

Generate outbound signing requests using these APIs in the following order:

  1. HttpRequestData: Build the API request.
  2. AuthCredential: Create a credential object or update an existing one. Use the credential to sign the request through the RequestAuthAPI class.
  3. RequestAuthAPI: Sign the request and return an HttpRequestAuthedData object.
  4. HttpRequestAuthedData: Get information about the signed request.
  5. GlideHTTPRequest: Send the signed request.

 

The documentation provides code sample for steps 1 to 4, but not does not provide guidance on how to pass the HttpRequestAuthedData object from step 4 into GlideHTTPRequest in step 5 to actually make the api call . Has anyone done that ? would appreciate if someone can share sample code. Thanks.

 

2 REPLIES 2

omenyayl
Tera Contributor
// your sn_auth.HttpRequestData object
var httpRequestData;

// your sn_auth.HttpRequestAuthedData object
var signedRequest;

// Build RESTMessageV2
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setEndpoint(httpRequestData.getEndpoint());
restMessage.setHttpMethod(httpRequestData.getHttpMethod());
// set other properties like body, headers, etc.

// sets headers like Authorization
Object.entries(signedRequest.getHeaderMap()).forEach(function(entry) {
    var headerName = entry[0];
    var headerValue = entry[1];
    restMessage.setRequestHeader(headerName, headerValue);
});

// Optionally set an AWS session token if you have one
var sessionToken;
restMessage.setRequestHeader('X-Amz-Security-Token', sessionToken);

gs.info(restMessage.execute().getBody());

omenyayl
Tera Contributor

Once you have an HttpRequestAuthedData object, call getHeaderMap() to get a set of calculated headers (like "Authorization", and add them to a sn_ws.RESTMessageV2 object via the setRequestHeader() method. Then, call the execute() method in sn_ws.RESTMessageV2 to send the HTTP request

 

var SigV4RequestUtils = Class.create();
SigV4RequestUtils.prototype = {
    initialize: function() {},

	/**
	 * @Param {string} path - The request path, i.e. /books
	 * @Param {string} method - The HTTP request method, i.e. GET
	 * @returns {sn_ws.RESTMessageV2} The rest message that can be used to send a signed request
	 */
    buildRequest: function(path, method) {
        var serviceConnectionAliasSysId = '1bfd23db14b23f43fd1dbf234fdfb2d1';
        var connectionInfoProvider = new sn_cc.ConnectionInfoProvider();
        var credentialsProvider = new sn_cc.StandardCredentialsProvider();
        var connectionInfo = connectionInfoProvider.getConnectionInfo(serviceConnectionAliasSysId);
        var authCredentials = credentialsProvider.getAuthCredentialByID(connectionInfo.getCredentialAttribute('sys_id'));

        // Build request data
        var httpRequestData = new sn_auth.HttpRequestData();
        httpRequestData.setEndpoint(connectionInfo.getAttribute('connection_url') + path);
        httpRequestData.setService('execute-api');
		httpRequestData.setRegion('us-west-2');
        httpRequestData.setHttpMethod(method);

        // Sign request
        var signingAPI = new sn_auth.RequestAuthAPI(httpRequestData, authCredentials);
        var signedRequest = signingAPI.generateAuth(signingAPI);

        // Build RESTMessageV2
        var restMessage = new sn_ws.RESTMessageV2();
        restMessage.setEndpoint(httpRequestData.getEndpoint());
        restMessage.setHttpMethod(httpRequestData.getHttpMethod());

        // If you have a session token, add it like this:
        // restMessage.setRequestHeader('X-Amz-Security-Token', sessionToken);
        
        Object.entries(signedRequest.getHeaderMap()).forEach(function(entry) {
            var headerName = entry[0];
            var headerValue = entry[1];
            restMessage.setRequestHeader(headerName, headerValue);
        });

		return restMessage;
    },

    type: 'SigV4RequestUtils'
};

gs.info(new SigV4RequestUtils().buildRequest('/books', 'GET').execute().getBody());