获取 User Token
User Token 用于代表特定用户执行操作,需要通过 OAuth 2.0 授权流程获取。
授权流程
步骤一:构造授权 URL
https://api.im.example.com/oauth/authorize?
client_id=cli_xxxxxx&
redirect_uri=https://your-app.com/callback&
response_type=code&
scope=user:read
步骤二:用户授权
用户在 IM 中确认授权后,会重定向到 redirect_uri,并携带 code 参数:
https://your-app.com/callback?code=abc123
步骤三:换取 Token
应用后端使用 code 换取 User Token:
curl -X POST 'https://api.im.example.com/v1/auth/user-token' \
-H 'Content-Type: application/json' \
-d '{
"app_id": "cli_xxxxxx",
"app_secret": "xxxxxx",
"code": "abc123"
}'
响应示例
{
"code": 0,
"data": {
"access_token": "u-xxxxxx",
"refresh_token": "r-xxxxxx",
"expires_in": 7200,
"user_id": "user_001"
}
}