留言板制作利⽤Python调⽤海康威视综合管理平台openAPI接⼝
前⾔:
KU波可调电衰减器对海康威视综合管理平台iSecureCenter进⾏⼆次开发,主要想获取直播流地址,需要进⾏⼆次开发。但是官⽹给出的Demo没有Python 版本,这⾥给出调⽤⽰例。 核⼼问题:
获取到指定接⼝的签名(CA认证),然后才能正常调⽤各个接⼝,⾃⼰也⾛了⼀些弯路。主要问题在: 1、⽣成签名拼接的字符串,拼接的不对导致出了验证失败;
2、得到了签名不知道怎么⽤,主要问题还是POST请求的各参数不明⽩真正含义;
3、平台对应的各端⼝需要开通;
4、多利⽤官⽹给出的⼩⼯具进⾏接⼝测试。
参考链接:
# -*- coding: utf-8 -*-
# _author_ =
电杆钢模# Email:
"""
协议:仅⽀持 HTTPS 传输
url格式:{hostname}:{port}/artemis/{uri}
# AK\SK摘要认证
调⽤ API 时,如果API需要安全认证,⾸先需要获取API的授权,得到AppKey和AppSecret;
其次,拼接签名字符串,将计算后的签名放在请求的 Header 传⼊,⽹关会通过对称计算签名来验证请求者的⾝份。 """
import os
import base64
import json
import time
import uuid
风琴式导轨防护罩import hmac # hex-based message authentication code 哈希消息认证码
import hashlib # 提供了很多加密的算法
97sdd
import requests
base_url = "ip:4432" # 可以正常访问的IP地址
# 注意增加/artemis
api_get_address_url = "/artemis/api/video/v2/cameras/previewURLs"
appKey = "2*****"
appSecret = "HjjuaNJGsv*******"
http_method = "POST"
def sign(key, value):
核桃剥壳机
temp = de(), de(), digestmod=hashlib.sha256)
return base64.b64encode(temp.digest()).decode()
x_ca_nonce = str(uuid.uuid4())
x_ca_timestamp = str(int(round(time.time()) * 1000))
# sign_str 的拼接很关键,不然得不到正确的签名
sign_str = "POST\n*/*\napplication/json" + "\nx-ca-key:" + appKey + "\nx-ca-nonce:" + \
x_ca_nonce + "\nx-ca-timestamp:" + x_ca_timestamp + "\n" + \
api_get_address_url
signature = sign(appSecret, sign_str)
print("[INFO] 获取到的签名值为:", signature)
headers = {
"Accept": "*/*",
"Content-Type": "application/json",
"x-ca-key": appKey, # appKey,即 AK
"x-ca-signature-headers": "x-ca-key,x-ca-nonce,x-ca-timestamp",
"x-ca-signature": signature, # 需要计算得到的签名,此处通过后台得到
"x-ca-timestamp": x_ca_timestamp, # 时间戳
"x-ca-nonce": x_ca_nonce # UUID,结合时间戳防重复
}
# 获取监控点的在线流样例
body = {
"cameraIndexCode": "36e0421391f7****",
"streamType": 0,
"protocol": "rtsp",
}
url = base_url + api_get_address_url
results = requests.post(url, data=json.dumps(body), headers=headers, verify=False) print(results)
print(results.json()
# {'code': '0', 'msg': 'success', 'data': {'url': 'rtsp://ip:554/openUrl/Kp0VqXS'}}