The provided official examples, while detailed, can be challenging for someone new to integrating with third-party platforms. After extensive testing and experimentation, I have documanted the process of generating the required signature. This serves as a reference for future use.
Authentication Method:
- AK/SK Digest Authentication
public static void testSignature() {
try {
// Request details
String requestUrl = "http://www.example.com/artemis/api/example?qa=a&qb=B";
String httpMethod = "POST";
String appKey = "29666671";
String appSecret = "empsl21ds3";
String xCaTimestamp = "1479968678000";
// Headers
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "*/*");
headers.put("Accept-Encoding", "gzip, deflate, sdch");
headers.put("Accept-Language", "zh-CN,zh;q=0.8");
headers.put("Connection", "keep-alive");
headers.put("Content-Length", "0");
headers.put("Content-Type", "text/plain;charset=UTF-8");
headers.put("Cookie", "JSESSIONID=D9C4A515CACAC31211D1612039D062B7");
headers.put("header-A", "A");
headers.put("header-B", "b");
headers.put("X-Ca-Key", appKey);
headers.put("X-Ca-Signature-Headers", "header-a,header-b,x-ca-key,x-ca-timestamp");
headers.put("X-Ca-Timestamp", xCaTimestamp);
headers.put("X-Requested-With", "XMLHttpRequest");
// Query parameters
Map<String, String> queryParameters = new HashMap<>();
queryParameters.put("qa", "a");
queryParameters.put("qb", "B");
// Body form
Map<String, String> bodyForm = new HashMap<>();
bodyForm.put("a-body", "a");
bodyForm.put("x-body", "x");
// Signature string
StringBuilder stringToSignBuilder = new StringBuilder();
stringToSignBuilder.append(httpMethod).append("\n")
.append(headers.get("Accept")).append("\n")
.append(headers.get("Content-Type")).append("\n")
.append("header-a:A\n")
.append("header-b:b\n")
.append("x-ca-key:").append(appKey).append("\n")
.append("x-ca-timestamp:").append(xCaTimestamp).append("\n")
.append("/artemis/api/example?a-body=a&qa=a&qb=B&x-body=x");
String stringToSign = stringToSignBuilder.toString();
// App secret
String secret = appSecret;
// HMAC SHA256 encryption
Mac hmacSHA256 = Mac.getInstance("HmacSHA256");
byte[] keyBytes = secret.getBytes("UTF-8");
hmacSHA256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
// Base64 encoding
String sign1 = Base64.getEncoder().encodeToString(hmacSHA256.doFinal(stringToSign.getBytes("UTF-8")));
byte[] bytes = org.apache.commons.codec.binary.Base64.encodeBase64(hmacSHA256.doFinal(stringToSign.getBytes("UTF-8")));
String sign2 = new String(bytes, "UTF-8");
// Official signature result: JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI=
System.out.println(sign1); // JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI=
System.out.println(sign2); // JRpUpk1ETjzr5gsbo4qoEA9EiQPejvNz12B837xV5HI=
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
Once the sign is generated, it can be used to populate the request headers and send the request.
Example of request headers:
// Sample request headers
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "*/*");
headers.put("Content-Type", "application/json");
headers.put("x-ca-key", "00000000");
headers.put("x-ca-signature-headers", "x-ca-key");
headers.put("x-ca-signature", "RHhiW7WMDZUEXmgjzJZMsLFJ8RWGR3+ffXIQI=");
After populating the headers, the request can be sent.
- Access Token Authentication
(Not covered, to be continued)
- About Authentication
Hikvision provides a security authentication libray that can be included in your project. By configuring the necessary parameters and using the provided tools, you do not need to manually generate signatures. Here is an excerpt from the official documentasion:
// Steps to integrate Hikvision's OpenAPI security library in Java
1. Create a new Java project and download the "OpenAPI security authentication library (Java)" from the Hikvision AI Cloud open platform website (https://open.hikvision.com/download/). Add the artemis-http-client.jar to your project. The library is also available in Maven:
<dependency>
<groupId>com.hikvision.ga</groupId>
<artifactId>artemis-http-client</artifactId>
<version>1.1.3</version>
</dependency>
2. Set up authentication information by configuring ArtemisConfig with the host, appKey (AK), and appSecret (SK).
3. Set the API URL.
4. Set the API parameters.
5. Call the API using the doPostStringArtemis method, which internally handles the authentication logic. Pass the API URL, parameters, and data submission type to complete the API call.