5 分钟接入示例

通过完整的 curl 命令,在 5 分钟内跑通用户注册绑定 → 快照查询 → 家庭成员添加 的完整链路,验证凭证与签名是否正确。

开始之前

确保你已拥有:

  • 沙箱应用的 App IDAppKeyAppSecret
  • 已安装 curlopenssl(macOS / Linux 自带)

下面脚本中,将以下变量替换为你的实际值:

APP_ID="100023"
APP_KEY="ak_live_xxxxxxxxxxxxxxxx"
APP_SECRET="sk_live_xxxxxxxxxxxxxxxx"
BASE_URL="https://sandbox-api.xiaobaotop.com"

签名函数(bash):

# 生成签名:md5(app_key + timestamp + app_secret + body)
make_sig() {
  local body="$1"
  local ts=$(date +%s)
  local raw="${APP_KEY}${ts}${APP_SECRET}${body}"
  local sig=$(echo -n "$raw" | openssl md5 | awk '{print $2}')
  echo "$ts $sig"
}

第一步:注册绑定用户

将合作方用户(channel_user_id)与晓葆平台账号绑定。 接口幂等,重复调用不产生重复数据。

BODY='{"channel_user_id":"demo_user_001","user":{"name":"张三","gender":"male","birth_date":"1985-06-15","phone":"13800138000"}}'

# 计算签名
TS=$(date +%s)
SIG=$(echo -n "${APP_KEY}${TS}${APP_SECRET}${BODY}" | openssl md5 | awk '{print $2}')

curl -s -X POST "${BASE_URL}/open/v1/channel/users" \
  -H "Content-Type: application/json" \
  -H "X-App-Id: ${APP_ID}" \
  -H "X-Timestamp: ${TS}" \
  -H "X-Signature: ${SIG}" \
  -d "${BODY}" | python3 -m json.tool

成功响应(201 Created):

{
  "success": true,
  "code": 0,
  "message": "绑定成功",
  "data": {
    "open_user_id": 10086,
    "channel_user_id": "demo_user_001",
    "created": true
  }
}
重要:将响应中的 open_user_id 持久化存储到合作方系统, 它是后续所有接口的路径参数。

第二步:查询用户快照

使用上一步获取的 open_user_id(示例用 10086), 拉取该用户的完整信息快照。

OPEN_USER_ID=10086
BODY=""

TS=$(date +%s)
SIG=$(echo -n "${APP_KEY}${TS}${APP_SECRET}${BODY}" | openssl md5 | awk '{print $2}')

curl -s -X GET "${BASE_URL}/open/v1/channel/users/${OPEN_USER_ID}" \
  -H "X-App-Id: ${APP_ID}" \
  -H "X-Timestamp: ${TS}" \
  -H "X-Signature: ${SIG}" | python3 -m json.tool

成功响应(200 OK)包含用户信息、家庭组列表、家庭成员列表。

第三步:添加家庭成员

先创建一个家庭组,再在组内添加家庭成员:

# 1. 创建家庭组
BODY='{"app_group_id":501,"name":"张三家庭"}'
TS=$(date +%s)
SIG=$(echo -n "${APP_KEY}${TS}${APP_SECRET}${BODY}" | openssl md5 | awk '{print $2}')

curl -s -X POST "${BASE_URL}/open/v1/channel/users/${OPEN_USER_ID}/family-groups" \
  -H "Content-Type: application/json" \
  -H "X-App-Id: ${APP_ID}" \
  -H "X-Timestamp: ${TS}" \
  -H "X-Signature: ${SIG}" \
  -d "${BODY}" | python3 -m json.tool
# 从响应中取 family_group_id,示例为 2001

# 2. 添加家庭成员
BODY='{"app_user_id":"demo_member_001","app_group_id":501,"name":"李四","relationship":"SPOUSE","gender":"female","birth_date":"1988-03-20"}'
TS=$(date +%s)
SIG=$(echo -n "${APP_KEY}${TS}${APP_SECRET}${BODY}" | openssl md5 | awk '{print $2}')

curl -s -X POST "${BASE_URL}/open/v1/channel/users/${OPEN_USER_ID}/family-members" \
  -H "Content-Type: application/json" \
  -H "X-App-Id: ${APP_ID}" \
  -H "X-Timestamp: ${TS}" \
  -H "X-Signature: ${SIG}" \
  -d "${BODY}" | python3 -m json.tool

验证成功

再次调用快照接口,确认家庭组和成员已正确写入:

# GET 同第二步,family_groups 和 family_members 数组应包含刚才写入的数据

如果三步均返回 "success": true,说明凭证和签名配置正确,可以正式开始业务开发。

下一步