Python3調用示例
更新時間 2025-04-24 15:36:55
最近更新時間: 2025-04-24 15:36:55
分享文章
本文為Python3調用示例
import hmac
import base64
import hashlib
import json
import time
import uuid
import requests
from urllib.parse import urlparse
def sha256 (content):
x = hashlib.sha256()
x.update(content.encode())
return x.hexdigest().upper()
def hmac_sha256 (key, content):
sign = hmac.new(key, content, digestmod="sha256").digest()
ret = base64.b64encode(sign)
return ret
# 計算簽名
def get_signature (ak, sk, app_key, params):
*# 創建待簽名字符串*
*# 一、header部分*
*# 主要包括3個header需要作為簽名內容:appkey、ctyun-eop-request-id、eop-date*
*# 1. 首先通過uuid生成ctyun-eop-request-id*
request_id = str(uuid.uuid1())
*# 2. 獲取當前時間戳并對時間進行格式化*
now_time = time.localtime()
eop_date = time.strftime("%Y%m%dT%H%M%SZ", now_time)
eop_date_simple = time.strftime("%Y%m%d", now_time)
*# 3. 對header部分按照字母順序進行排序并格式化*
camp_header = "appkey:{0}\nctyun-eop-request-id:{1}\neop-date:{2}\n".format(app_key, request_id, eop_date)
*# 二、query部分*
*# 對url的query部分進行排序*
parsed_url = urlparse(request_url)
query = parsed_url.query
query_params = **sorted** (query.split("&"))
after_query = ""
for query_param in query_params:
if **len** (after_query) < 1:
after_query += query_param
else:
after_query += "&" + query_param
*# 三、body參數進行sha256摘要*
*# sha256 body*
content_hash = sha256(json.dumps(params)).lower()
*# 完成創建待簽名字符串*
pre_signature = camp_header + "\n" + after_query + "\n" + content_hash
*# 構造動態密鑰*
k_time = hmac_sha256(sk.encode("utf-8"), eop_date.encode("utf-8"))
k_ak = hmac_sha256(base64.b64decode(k_time), ak.encode("utf-8"))
k_date = hmac_sha256(base64.b64decode(k_ak), eop_date_simple.encode("utf-8"))
*# 簽名的使用*
signature = hmac_sha256(base64.b64decode(k_date), pre_signature.encode("utf-8"))
*# 將數據整合得到真正的header中的內容*
sign_header = "{0} Headers=appkey;ctyun-eop-request-id;eop-date Signature={1}".format(ak, signature.decode())
*# 返回request-id eop-date和sign_header*
return request_id, eop_date, sign_header
# 向服務發送請求
def do_post (url, headers, params):
response = requests.post(url, data=json.dumps(params), headers=headers)
try:
**print** (response.status_code)
**print** (response.json())
except AttributeError:
**print** ("請求失敗")
if name == 'main':
*# 請求地址,替換為請求能力的地址*
request_url = "//ai-global.ctapi.daliqc.cn/v1/aiop/api/2f6hqix09mv4/face/PERSON/person/detectFaceFromBase64"
*# 官網accessKey,登錄云網門戶,在“賬號中心”->“安全設置”->“登錄保護”->“用戶AccessKey”點擊“查看”獲取。*
ctyun_ak = 'accessKey'
*# 官網securityKey,登錄云網門戶,在“賬號中心”->“安全設置”->“登錄保護”->“用戶AccessKey”點擊“查看”獲取,securityKey只在創建時展示*
ctyun_sk = 'securityKey'
*#產品控制臺(//ai.daliqc.cn/console/apps/list)-我的應用中獲取的appKey*
ai_app_key = 'appKey'
*# body內容從本地文件中獲取*
*# 打開圖片文件*
f = **open** (r'test.jpeg', 'rb')
img_base64 = base64.b64encode(f.read()).decode()
*# body內容,替換為請求能力的參數,具體參數參考能力的API文檔*
params = {"imageContent": img_base64}
*# 調用get_signature方法獲取簽名*
request_id, eop_date, sign_header = get_signature(ctyun_ak, ctyun_sk, ai_app_key, params)
*# 生成請求header*
*# 請求header*
headers = {
'Content-Type': 'application/json;charset=UTF-8',
'ctyun-eop-request-id': request_id,
'appkey': ai_app_key,
'Eop-Authorization': sign_header,
'Eop-date': eop_date,
'Host': 'ai-global.ctapi.daliqc.cn'
}
**print** ("請求頭部:")
**print** (headers)
*# 執行post請求*
do_post(request_url, headers, params)