API Help
HSNM administration backend general discussion

Rate this topic
   Post Reply
3 posts   •   Page 1 of 1
tylerlane
Posts: 2
Joined: 03 Mar 2022, 03:10

API Help

by tylerlane » 06 May 2022, 20:37

Hello

Code: Select all

<?php class RESTfulAPI { function __construct($DomainOrIP, $Key, $Secret) { $this->BaseUri = $DomainOrIP ."/api/v2/"; $this->ApiKey = $Key; $this->ApiSecret = $Secret; // Encription vector initialization $this->SecretIV = substr(hash("SHA256", $this->ApiKey, true), 0, 16); } function base64Url_Encode($data) { return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } // Encription for properties function APIEncryptData($Data) { $output = openssl_encrypt($Data, "AES-256-CBC", md5($this->ApiSecret), OPENSSL_RAW_DATA, $this->SecretIV); return $this->base64Url_Encode($output); } function APICall($Endpoint, $Data) { try { $Options = array('http'=> array( 'method'=>'POST', 'header'=>'Content-type: application/x-www-form-urlencoded', 'content'=>'data=' .$this->APIEncryptData($Data) ) ); $context = stream_context_create($Options); $RetVal = file_get_contents($this->BaseUri .$Endpoint."/apikey=" .$this->ApiKey, false, $context); } Catch (Exception $e) { $RetVal = '{"warning":"","error":"Generic error"}'; } return $RetVal; } } //ResellerLevel //$ApiKey = ""; //$ApiSecret = ""; //admin api key $ApiKey = ""; $ApiSecret = ""; $API = new RESTfulAPI("https://hotspot.campcomm.net", $ApiKey, $ApiSecret); // $Endpoint = 'userWrite'; $Data = '{"id":"0","DomainID":"9","Username":"101","Password":"smith","AddProduct":"1","ProductID":"49"}'; //$Endpoint='userRead'; //$Data = '{"id":2251}'; $JSonRetVal = $API->APICall($Endpoint, $Data); //if (isset($RetVal["error"]) && $RetVal["error"] != "") { // echo "Error: " .$RetVal["error"]; //} else { print_r($JSonRetVal); ?>
I'm getting the following error.
{"error":"Error! You have not defined a domain","id":"0"}

If I remove the DomainID keypair I get the same error,
If I use the "reseller" level API key I get
{"error":"Not authorized"}

any helpful hints to get this working, the sample code on the documentation isn't agreeing with me.

tylerlane
Posts: 2
Joined: 03 Mar 2022, 03:10

Re: API Help

by tylerlane » 06 May 2022, 22:26

Code: Select all

$Data = '{"id":"0","DomainID":"5","UserName":"Cabin101","Password":"smith","AddProduct":"1","ProductID":"49"}';

Had a few errors in the data line.

Wrong DomainId and UserName I had typed as Username

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

Re: API Help

by HSNMSupport » 09 May 2022, 08:37

Hi Tyler,
what is the purpose of your code?
do you need to add a user or a product to a user?

Rate this topic

Who is online

Users browsing this forum: No registered users and 1 guest

It is currently 02 Jul 2022, 14:14