Device Code
适用于电视、机顶盒、CLI、打印机等难以完成完整浏览器重定向的设备。设备展示用户码,用户在另一台设备打开验证页完成登录与授权。
相关端点
| 用途 | 地址 |
|---|---|
| Device Authorization | https://login.heanbian.com/oauth2/device_authorization |
| 用户验证页 | https://login.heanbian.com/activate |
| Token | https://login.heanbian.com/oauth2/token |
流程
- 设备调用 Device Authorization 端点,获得
device_code、user_code、verification_uri、interval等。 - 向用户展示
user_code与验证地址(或二维码)。 - 用户在浏览器打开
https://login.heanbian.com/activate,输入用户码并登录授权。 - 设备按
interval轮询 Token 端点(grant_type=urn:ietf:params:oauth:grant-type:device_code)。 - 用户完成授权后,轮询返回 access_token(及可选 refresh_token / id_token)。
1. 申请设备码
POST
https://login.heanbian.com/oauth2/device_authorization
curl -X POST \
-d 'client_id=<client_id>' \
-d 'scope=openid profile' \
https://login.heanbian.com/oauth2/device_authorization
2. 轮询换票
POST
https://login.heanbian.com/oauth2/token
curl -u '<client_id>:<client_secret>' \
-d 'grant_type=urn:ietf:params:oauth:grant-type:device_code' \
-d 'device_code=<device_code>' \
https://login.heanbian.com/oauth2/token
轮询提示:在用户尚未完成授权时,常见响应为
authorization_pending 或 slow_down。请遵守返回的间隔,避免过于频繁请求。
接入注意
- 客户端须支持设备码 grant(创建时启用相应授权类型)。
- 用户验证页也可直接访问:https://login.heanbian.com/activate。
- 机密客户端换票时仍需提供客户端认证。