示例代码

本文以网页审核任务提交接口为例,演示如何生成签名并发起请求。

接口说明请参考:网页检测任务提交

签名规则请参考:请求签名

curl 请求示例

curl -X POST 'https://msafe.ilivedata.com/api/v1/media/web/submit' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Accept: application/json;charset=UTF-8' \
  -H 'X-AppId: 82100001' \
  -H 'X-TimeStamp: 2026-01-31T07:59:03Z' \
  -H 'Authorization: *****' \
  -d '{"url":"https://example.com/page.html","strategyId":"DEFAULT"}'

其中 Authorization 需要按请求体、请求路径、X-AppIdX-TimeStamp 计算得到,不能直接使用示例中的 *****

Python 示例

以下示例使用 Python 标准库生成签名并提交网页审核任务。

import base64
import hashlib
import hmac
import json
import urllib.request
from datetime import datetime, timezone


APP_ID = "82100001"
SECRET_KEY = "your_secret_key"
HOST = "msafe.ilivedata.com"
URI = "/api/v1/media/web/submit"
URL = f"https://{HOST}{URI}"

body = {
    "url": "https://example.com/page.html",
    "strategyId": "DEFAULT",
}

json_body = json.dumps(body, separators=(",", ":"), ensure_ascii=False)
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
body_hash = hashlib.sha256(json_body.encode("utf-8")).hexdigest()

string_to_sign = "\n".join([
    "POST",
    HOST,
    URI,
    body_hash,
    f"X-AppId:{APP_ID}",
    f"X-TimeStamp:{timestamp}",
])

authorization = base64.b64encode(
    hmac.new(
        SECRET_KEY.encode("utf-8"),
        string_to_sign.encode("utf-8"),
        hashlib.sha256,
    ).digest()
).decode("utf-8")

request = urllib.request.Request(
    URL,
    data=json_body.encode("utf-8"),
    method="POST",
    headers={
        "Content-Type": "application/json;charset=UTF-8",
        "Accept": "application/json;charset=UTF-8",
        "X-AppId": APP_ID,
        "X-TimeStamp": timestamp,
        "Authorization": authorization,
    },
)

with urllib.request.urlopen(request, timeout=10) as response:
    print(response.status)
    print(response.read().decode("utf-8"))

成功响应示例:

{
  "errorCode": 0,
  "taskId": "task_**************************",
  "status": "PROCESSING"
}