Dear HSNM Support,
Have a good Day,
We just want to ask for support regarding our HSNM Connection via API V2. Still, we are using Version 1 due to an error when we are using API V2.
We are trying to make a connection and test with API V2 since we are using V1 till now
But every time we have an error.
Below is the Script and code error.
Error: {"error":"Error! Parameters error. The data parameter is not in json format"}
///////////////////////
import base64
import hashlib
from Crypto.Cipher import AES
import requests
import json
class RESTfulAPI:
def __init__(self, domain_or_ip, key, secret):
self.base_uri = f"{domain_or_ip}/api/v2/"
self.api_key = key
self.api_secret = secret
self.secret_iv = hashlib.sha256(self.api_key.encode()).digest()[:16]
def base64_url_encode(self, data):
return base64.urlsafe_b64encode(data).rstrip(b'=').decode()
def api_encrypt_data(self, data):
cipher = AES.new(hashlib.md5(self.api_secret.encode()).digest(), AES.MODE_CBC, self.secret_iv)
padded_data = data + (16 - len(data) % 16) * chr(16 - len(data) % 16)
encrypted = cipher.encrypt(padded_data.encode())
return self.base64_url_encode(encrypted)
def api_call(self, endpoint, data):
try:
encrypted_data = self.api_encrypt_data(data)
response = requests.post(
f"{self.base_uri}{endpoint}/apikey={self.api_key}",
data={'data': encrypted_data},
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
return response.text
except Exception as e:
return json.dumps({"warning": "", "error": "Generic error"})
# Example usage with provided credentials
api_key = **************************************"
api_secret = "*************************************"
api = RESTfulAPI("http://172.16.16.2", api_key, api_secret)
endpoint = "userRead"
data = json.dumps({"id": "141"})
json_ret_val = api.api_call(endpoint, data)
# Print response
if "error" in json_ret_val:
print(f"Error: {json_ret_val}")
else:
print(json_ret_val)
///////////////////////////////////////////////////////////
If you have some sample API for V2 much appreciated if u can share with us. We are using Python.
Thanks,
Magenta