/
HCP OpenAPI's Headers Configuration

HCP OpenAPI's Headers Configuration

This page is writing about the correct steps to configure the required request headers before sending an API request at HCP OpenAPI.

HikCentral Professional (HCP) v2.4.1 is used.

Prerequisite

  1. APPkey (header: X-Ca-Key)

  2. APPsecret (used for the formation of signature string)

  3. Signature String (header: X-Ca-Signature)

 

APPkey and APPsecret

Both APPkey and APPsecret can be found after creating a partner at Open Platform (https://IPaddress/artemis-web > Partner Management).

APPkey is included in the request header, while APPsecret is used for the calculation of signature string.

 

Signature String

  1. Form a string with the request method, header’s Accept, header’s Content-Type and URI, separated by “\n“

Example 1: POST /artemis/api/example?qa=value1&qb=value2&qc ; Accept = */* ; Content-Type=application/json

String: “POST\n*/*\napplication/json\n/artemis/api/example?qa=value1&qb=value2&qc

Example 2: POST /artemis/api/example ; Accept = application/json ; Content-Type=application/json

String: “POST\napplication/json\napplication/json\n/artemis/api/example

URI must start with “/“, containing the endpoint and request parameters.

  1. Convert the string with HmacSHA256 algorithm and APPsecret as the key, to generate message digest.

  2. Encode the message digest in Base64, to generate the signature string.

 

Prepare the request header

Use the APPkey and Signature String to form the request header.

{ 'X-Ca-Key': <APPkey>, 'X-Ca-Signature': <Signature String> }

 

Sample Code

Calculating Signature String

def generate_signature(app_key, app_secret, method, path): message = "POST\n*/*\napplication/json\n/artemis/api/example" # Initial string key_bytes = bytes(app_secret, 'utf-8') # key message_bytes = bytes(message, 'utf-8') # msg signature = hmac.new(key_bytes, msg=message_bytes, digestmod=hashlib.sha256).digest() # message digest encoded_signature = base64.b64encode(signature).decode('utf-8') # signature string return encoded_signature

Sending request

def make_signed_request(method, url, app_key, app_secret, request_body): signature = generate_signature(app_key, app_secret, method, url) headers = { 'X-Ca-Key': app_key, 'X-Ca-Signature': signature } response = requests.request(method, url, headers=headers, json=request_body, verify=False) return response

Example code from main

 

Calculation of signature and making the request’s header

 

Related content

HCP OpenAPI Technical Documentation
HCP OpenAPI Technical Documentation
More like this
Data Manipulation Method
Data Manipulation Method
Read with this
Enable OpenAPI (Open Platform) in HikCentral
Enable OpenAPI (Open Platform) in HikCentral
More like this
API User Authentication using CAS
API User Authentication using CAS
More like this
OpenID Connect With CAS User Guide
OpenID Connect With CAS User Guide
More like this

Copyright © Asia Pacific University. All Rights Reserved.