认证文档 | 大装置帮助中心
跳到主要内容

认证文档

  • 服务端使用 HMAC 方式进行身份认证,需要在请求Header中额外添加两个字段 X-DateAuthorization.

  • 用户需要先从SenseCore的控制台首页获取AccessKey和SecretKey,然后通过如下方式计算上述两个字段的值.

bash示例

#!/bin/bash

access_key='*****'
secret_key='*****'

xdate=$(LC_TIME=en_US date -u +"%a, %d %b %Y %H:%M:%S GMT")
data="x-date: ${xdate}"

# 生成签名
sign=$(echo -n "$data" | openssl dgst -sha256 -hmac "$secret_key" -binary | base64)
auth="hmac accesskey=\"${access_key}\", algorithm=\"hmac-sha256\", headers=\"x-date\", signature=\"${sign}\""

# 调试输出
echo "X-Date: $xdate"
echo "Authorization: $auth"

# 执行请求
curl 'https://aidmp.cn-sh-01.sensecoreapi.cn/studio/rag/data/v1/applications/3e0b067fb6964d23b967d01f2133a5ab/releases:show' \
-H "X-Date: $xdate" \
-H "Authorization: $auth" \
-H "Content-Type: application/json" \
-X GET

上述命令需使用bash/zsh执行,若使用sh执行,得到的签名值会有误

python 示例

import hmac
import hashlib
import base64
from datetime import datetime, timezone


def gen_auth_header(access_key: str, secret_key: str):
"""
生成包含 X-Date 和 Authorization 的 HTTP 头部。

:param access_key: API 的 Access Key
:param secret_key: API 的 Secret Key
:return: 包含 X-Date 和 Authorization 的字典
"""
# 生成 X-Date 时间戳
x_date = datetime.now(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S GMT")

# 构造签名内容
sign_content = f"x-date: {x_date}"

# 生成签名
signature = base64.b64encode(
hmac.new(secret_key.encode(), sign_content.encode(), hashlib.sha256).digest()
).decode()

# 构造 Authorization 头
auth = (
f'hmac accesskey="{access_key}", algorithm="hmac-sha256", '
f'headers="x-date", signature="{signature}"'
)

return {
"X-Date": x_date,
"Authorization": auth,
}


# 示例调用
if __name__ == "__main__":
# 示例 Access Key 和 Secret Key
access_key = "****"
secret_key = "****"

# 调用函数生成头部
headers = gen_auth_header(access_key, secret_key)

# 打印结果
print("Generated Headers:")
for key, value in headers.items():
print(f"{key}: {value}")

# 示例请求
import requests

url = "https://aidmp.cn-sh-01.sensecoreapi.cn/studio/rag/data/v1/applications/3e0b067fb6964d23b967d01f2133a5ab/releases:show"
response = requests.get(url, headers=headers)

print("\nResponse:")
print(f"Status Code: {response.status_code}")
print(f"Response Body: {response.text}")

golang 示例

package main

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"net/http"
"time"
)

// genAuthHeader 生成 HTTP 头部
func genAuthHeader(accessKey, secretKey string) (map[string]string, error) {
xDate := time.Now().UTC().Format(http.TimeFormat)
signContent := "x-date: " + xDate

mac := hmac.New(sha256.New, []byte(secretKey))
mac.Write([]byte(signContent))
signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))

auth := fmt.Sprintf(
`hmac accesskey="%s", algorithm="hmac-sha256", headers="x-date", signature="%s"`,
accessKey, signature,
)

return map[string]string{
"X-Date": xDate,
"Authorization": auth,
}, nil
}

func main() {
accessKey := "****"
secretKey := "****"

headers, err := genAuthHeader(accessKey, secretKey)
if err != nil {
fmt.Println("Error generating headers:", err)
return
}

url := "https://aidmp.cn-sh-01.sensecoreapi.cn/studio/rag/data/v1/applications/3e0b067fb6964d23b967d01f2133a5ab/releases:show"
req, _ := http.NewRequest("GET", url, nil)
for key, value := range headers {
req.Header.Set(key, value)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)
fmt.Printf("Status Code: %d\nResponse Body: %s\n", resp.StatusCode, string(body))
}