ContributionsMost RecentMost LikesSolutionsauthentication failing in ServiceNow getting error 1401 Hi Team, I trying to get the devices from the logicmonitor into servicenow, it is throwing an error authentication failed. {"data":null,"errmsg":"Authentication failed","status":1401} I have compared postman and token and below script token everything is perfect. Below is the script. can you please help me where is the mistake. var ACCESS_ID = 'test'; var ACCESS_KEY = 'abc'; var ACCOUNT_NAME = 'cde'; var epoch = (new Date()).getTime(); var id = 2; var resourcePath = '/device/devices'; var data= ''; var requestVars = 'GET' + epoch + resourcePath; var HexUtil = { convertByteArrayToHex : function(byteArray) { var hex = ""; byteArray.forEach(function(byteValue) { hex += HexUtil.convertByteToHex(byteValue); }); return hex; }, convertByteToHex : function(b) { var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "a", "b", "c", "d", "e", "f"]; return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f]; } }; var key = "lma_Bmz]H_625^5H[EQi9U627pdg}8yy(-56X-hsiL7~e^s)9~56ee+^Mh)i3DKXLODZmNTRhNTUtMDM2MC00ZTY5LTkyNWItZGIwNjZmMDMyZTg5L4kmWbA"; key = encodeURIComponent(key); key = GlideStringUtil.base64Encode(key); var msg = requestVars; msg = encodeURIComponent(msg); var mac = new GlideCertificateEncryption(); signature = mac.generateMac(key, "HmacSHA256", msg); gs.print(signature); // Yes! bp7ym3X//Ft6uuUn1Y/a2y/kLnIZARl2kXNDBl9Y7Uo= var bytes = GlideStringUtil.base64DecodeAsBytes(signature); gs.print(JSON.stringify(bytes)); gs.print(bytes.length); // Yes! [110,-98,-14,-101,117,-1,-4,91,122,-70,-27,39,-43,-113,-38,-37,47,-28,46,114,25,1,25,118,-111,115,67,6,95,88,-19,74] var hex = HexUtil.convertByteArrayToHex(bytes); gs.print(hex); // Yes! 6e9ef29b75fffc5b7abae527d58fdadb2fe42e7219011976917343065f58ed4a var hexB64 = GlideStringUtil.base64Encode(hex); gs.print(hexB64); var token = 'LMv1 ' + ACCESS_ID + ':' + hexB64 + ':' + epoch; gs.info('Devices = ' +token); var request = new sn_ws.RESTMessageV2(); request.setEndpoint('https://cde.logicmonitor.com/santaba/rest/device/devices'); request.setHttpMethod('GET'); request.setRequestHeader('Authorization', token); //request.setRequestHeader("Accept", "application/json"); request.setRequestHeader("Content-Type", 'application/json'); var response = request.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); gs.log("test==="+httpStatus+responseBody); if (httpStatus == 200) { var temJson = JSON.parse(responseBody); var aTA = temJson.data; gs.log("aTA ==="+aTA); } Thank You, Sai. SolvedRe: Unable to authenticate Rest api with servicenow to get devices Hi Adam, I tried working on this actually issue is here with cryptoJS which is not available by out of box. I have created the same in Servicenow scoped application script include but still it is not working. when I use the above script getting the signature length less and due to this authentication is not working. Unable to authenticate Rest api with servicenow to get devices Hi All, I am trying to authenticate in servicenow using script but it is not working. It is throwing an error authentication failed status 401. var ACCESS_ID = '123'; var ACCESS_KEY = 'abc'; var ACCOUNT_NAME = 'test'; var resourcePath = '/device/devices'; var epoch = (new Date()).getTime(); var id = 2; var requestVars = 'GET' + epoch + resourcePath; var HexUtil = { convertByteArrayToHex : function(byteArray) { var hex = ""; byteArray.forEach(function(byteValue) { hex += HexUtil.convertByteToHex(byteValue); }); return hex; }, convertByteToHex : function(b) { var hexChar = ["0", "1", "2", "3", "4", "5", "6", "7","8", "9", "a", "b", "c", "d", "e", "f"]; return hexChar[(b >> 4) & 0x0f] + hexChar[b & 0x0f]; } }; // Compute the HMACSHA256 hash using GlideDigest var key = "key"; key = encodeURIComponent(key); key = GlideStringUtil.base64Encode(key); var msg = "message"; msg = encodeURIComponent(msg); var mac = new GlideCertificateEncryption(); signature = mac.generateMac(requestVars, "HmacSHA256", ACCESS_ID); var bytes = GlideStringUtil.base64DecodeAsBytes(signature); var hex = HexUtil.convertByteArrayToHex(bytes); var hexB64 = GlideStringUtil.base64Encode(hex); var token = 'LMv1 ' + ACCESS_ID + ':' + signature + ':' + epoch; gs.info('Devices = ' +token); var httpRequest = new GlideHTTPRequest('https://' + ACCOUNT_NAME + '.logicmonitor.com/santaba/rest/device/devices'); httpRequest.addHeader('Content-Type', 'application/json'); httpRequest.addHeader('Authorization', token); //httpRequest.addHeader('x-server-version', '3'); var response = httpRequest.getBody(); gs.log(response); Could you please help me on this.
Top Contributionsauthentication failing in ServiceNow getting error 1401SolvedUnable to authenticate Rest api with servicenow to get devicesRe: Unable to authenticate Rest api with servicenow to get devices