使用凭证信息,生成指定IDaaS EIAM实例下的某个应用的access token。
接口说明
目前支持的方式:授权码模式、设备流、刷新 token、客户端模式、账密模式。
1. 授权码模式(Authorization Code)
适用场景:标准 OAuth 2.0 授权码流程,适用于有前端交互的 Web 应用。 调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code={授权码}
&redirect_uri={回调地址}
&client_id={客户端 ID}
&client_secret={客户端密钥}
参数说明:
● code:通过授权端点获取的授权码。
● redirect_uri:必须与获取 code 时使用的 redirect_uri 一致
1.1 公共客户端下的授权码模式
适用场景:适用于无法安全保存密钥的应用,如 SPA、原生应用。该流程中,无需使用 client_secret,但必须使用 PKCE 机制。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code={授权码}
&redirect_uri={回调地址}
&client_id={客户端 ID}
&code_verifier={code_verifier}
参数说明:
● code_verifier:PKCE 机制中的验证密钥,客户端在发起授权请求时生成,并用于派生 code_challenge。在换取 token 时,必须将此值原样提交,且必须与生成 code_challenge 时使用的值一致。
2. 设备流(Device Flow)。
适用场景:适用于电视、IoT 等输入受限设备。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code={设备码}
&client_id={客户端 ID}
&client_secret={客户端密钥}
获取设备码:需先调用/oauth2/device/code
获取 device_code 和 user_code
2.1 公共客户端下的设备流
适用场景:无法方便地进行交互式登录,且属于公共客户端。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code={设备码}
&client_id={客户端 ID}
3. 刷新 Token(Refresh Token)。
适用场景:使用 refresh_token 获取新的 access_token。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token={刷新令牌}
&client_id={客户端 ID}
&client_secret={客户端密钥}
4. 客户端模式(Client Credentials)。
适用场景:服务端间认证,无需用户参与。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id={客户端 ID}
&client_secret={客户端密钥}
&scope={权限范围}
5. 账密模式(Password)。
适用场景:传统用户名密码认证(需谨慎使用)。调用示例:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=password
&username={用户名}
&password={密码}
&client_id={客户端 ID}
&scope={权限范围}
调试
您可以在OpenAPI Explorer中直接运行该接口,免去您计算签名的困扰。运行成功后,OpenAPI Explorer可以自动生成SDK代码示例。
授权信息
请求语法
POST /v2/{instanceId}/{applicationId}/oauth2/token HTTP/1.1
请求参数
名称 | 类型 | 必填 | 描述 | 示例值 |
---|---|---|---|---|
instanceId | string | 是 | 实例 ID。 | idaas_ue2jvisn35ea5lmthk267xxxxx |
applicationId | string | 是 | 应用 ID。 | app_mkv7rgt4d7i4u7zqtzev2mxxxx |
client_id | string | 否 | 客户端 ID。 | app_mkv7rgt4d7i4u7zqtzev2mxxxx |
client_secret | string | 否 | 客户端密钥,赋值场景: 当 grant_type 为 client_credentials",采用 client_secret_post 方式时,需填写此值。 | CSEHDcHcrUKHw1CuxkJEHPveWRXBGqVqRsxxxx |
grant_type | string | 是 | 授权类型,目前支持的类型如下:
枚举值:
| client_credentials |
code | string | 否 | 授权 code 码,传入场景: grant_type=authorization_code 授权码模式时,必填参数。 | xxxx |
username | string | 否 | 账户名,在帐密模式下,必填参数。 | uesrname_001 |
password | string | 否 | 账户名,在帐密模式下,必填参数。 | xxxxxx |
device_code | string | 否 | 设备 code 码,传入场景: grant_type=urn:ietf:params:oauth:grant-type:device_code 即设备流模式时,必填参数。 | xxxx |
redirect_uri | string | 否 | 重定向 URI,传入场景: grant_type=authorization_code 授权码模式时,必须参数,与获取授权码请求中的重定向 URI 必须是一致的。 | xxx |
refresh_token | string | 否 | 刷新 token,传入场景: grant_type=refresh_token 即刷新令牌模式时,必填参数。 | ATxxx |
code_verifier | string | 否 | 授权码模式下,开启 PKCE 能力时,对应的验证 code。 | xxx |
exclusive_tag | string | 否 | 排除的 tag。 | ATxxx |
scope | string | 否 | scope 范围,非必须,支持多选,多个取值以空格分隔。 可选取值:
| xxxx |
返回参数
示例
正常返回示例
JSON
格式
{
"token_type": "Bearer",
"access_token": "ATxxx",
"refresh_token": "RTxxx",
"expires_in": 1200,
"expires_at": 1653288641,
"id_token": "xxxxx"
}
错误码
访问错误中心查看更多错误码。
变更历史
变更时间 | 变更内容概要 | 操作 |
---|---|---|
2023-10-24 | API 内部配置变更,不影响调用 | 查看变更详情 |
2023-04-04 | API 内部配置变更,不影响调用 | 查看变更详情 |