Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POST API to Verint, the connection is not getting authorised #489

Open
nasliya opened this issue May 19, 2023 · 1 comment
Open

POST API to Verint, the connection is not getting authorised #489

nasliya opened this issue May 19, 2023 · 1 comment

Comments

@nasliya
Copy link

nasliya commented May 19, 2023

Hi All,

I am trying to POST the data using generic CSI API of Verint( Workforce Mnagement Tool) using API Key and Value. Unfortunately, the connection si not getting established. When executed the code using postman , the POST Request got succedded because there isa documentation with some prerequisite script in postman. Can someone help in fixing the below code.

Python Code 👍

import requests

api_key = "xxxxxxxx"
api_value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

Set up the headers with the API key and value

headers = {
"Content-Type": "application/json",
"X-Api-Key": api_key,
"X-Api-Value": api_value
}

data

data = {
"data": {
"type": "queueHistoryTimeSeries",
"id": 1,
"attributes": {
"queueTimeSeries": {
"dataSourceName": "AZ_Services_Datasource",
"queueName": "AZS_Test2",
"time": "2023-01-07T08:00:00.000-0700",
"reportInterval": 30,
"dataSourceGroupTimeSeries": [
{
"dataSourceGroupName": "TestG2",
"volumeActual": 80,
"volumeHandledActual": 90,
"averageSpeedToAnswerActual": 90,
"activityHandlingTimeActual": 120,
"serviceLevelActual": 50,
"abandonsActual": 90,
"backlogActual": 90,
"occupancyActual": 90,
"staffingActual": 90
}
]
}
}
}
}

Set up the endpoint URL

url = "http://xxxxxxxxxxxxxxxxxx/timeSeriesData"

Send a GET request to the endpoint with the headers

response = requests.post(url, headers=headers,data=data)
print(response.request.url)
print(response.request.headers)
print(response.request.body)

@nasliya
Copy link
Author

nasliya commented May 19, 2023

PFB the prerequisite request used in postman"

var sdk = require('postman-collection');
var crypto = require("crypto-js");

//get api key and key id
var APIKeyId = pm.environment.get("api_key_id"),
APIKey = pm.environment.get("api_key");

//get ISO date
function ISODateString(d) {
function pad(n) {
return n < 10 ? '0' + n : n
}
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' +
pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' +
pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + 'Z'
}

//generate base64url by using cryptojs
function base64url(input) {
var base64String = crypto.enc.Base64.stringify(input);
return urlConvertBase64(base64String);
}

//convert base64url to base64
function debase64url (str) {
return (str + '==='.slice((str.length + 3) % 4))
.replace(/-/g, '+')
.replace(/_/g, '/')
}

//convert to base 64 url
function urlConvertBase64(input) {
var output = input.replace(/=+$/, '');
output = output.replace(/+/g, '-');
output = output.replace(///g, '_');

return output;
}

var replaceVars = function(string)
{
return string.toString().replace(/{{.+?}}/g, function(match)
{
var varName = match.substr(2, match.length - 4);
var varValue = environment[varName] || globals[varName];
return varValue ? replaceVars(varValue) : match; // recursive!
});
};

// get 32 bytes random;
var random = crypto.lib.WordArray.random(16);

//get path expanding any variables that exist
var fullPath = replaceVars(request.url);
var loc = new sdk.Url(fullPath);
var path = loc.getPath();

//generate canonicalizedHeader
var ref = pm.request.headers.toObject(true);
var canonicalizedHeader = "";

for (var key in ref) {

//only headers with "verint-" prefix can be used
if (key.substring(0, 7).toLowerCase() != "verint-") continue;

canonicalizedHeader += (key + ":");
var value = ref[key];

canonicalizedHeader += value;
canonicalizedHeader += "\n";
}

//make canonicalizedHeader it lower case
canonicalizedHeader = canonicalizedHeader.toLowerCase();

//get String to sign
var salt = base64url(random);
var method = pm.request.method;
var issuedAt = ISODateString(new Date());

var stringToSign = salt + "\n" + method + "\n" + path + "\n" + issuedAt +
"\n" + canonicalizedHeader + "\n";

var hash = crypto.HmacSHA256(stringToSign,
crypto.enc.Base64.parse(debase64url(APIKey)));

//get an signature
var signature = crypto.enc.Base64.stringify(hash);

//String prefix
var verintAuthId = "Vrnt-1-HMAC-SHA256";

//Generate Authorization Header Value
var authHeaderValue = verintAuthId + " " + "salt=" + salt + "," + "iat=" +
issuedAt + "," + "kid=" + APIKeyId + "," + "sig=" +
urlConvertBase64(signature);

postman.setGlobalVariable("Authorization", authHeaderValue);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant