API V2 Script Error
HSNM administration backend general discussion

Rate this topic
   Post Reply
3 posts   •   Page 1 of 1
Magenta
Posts: 50
Joined: 12 Mar 2019, 05:48

API V2 Script Error

by Magenta » 10 Sep 2024, 06:23

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

Magenta
Posts: 50
Joined: 12 Mar 2019, 05:48

Re: API V2 Script Error

by Magenta » 10 Sep 2024, 07:20

API2 parameter error.png
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
You do not have the required permissions to view the files attached to this post.

HSNMSupport
Posts: 1553
Joined: 26 Jul 2016, 09:16

Re: API V2 Script Error

by HSNMSupport » 10 Sep 2024, 08:57

Hi Magenta,
I am sorry but our Developers are not Python experts.
The problem probably lies in the data encryption.
Let's see if some Forum user can help you on this.
S.T.

Rate this topic

Who is online

Users browsing this forum: No registered users and 4 guests

It is currently 08 Oct 2024, 19:23