GenerateToken - 生成应用认证token

使用凭证信息,生成指定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 时使用的值一致。

code_verifier 和 code_challenge 生成的 Java 示例:

String codeVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(new SecureRandom().generateSeed(43));
String codeChallenge = Base64.getUrlEncoder().withoutPadding().encodeToString(java.security.MessageDigest.getInstance("SHA-256").digest(codeVerifier.getBytes()));

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代码示例。

调试

授权信息

当前API暂无授权信息透出。

请求语法

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 客户端模式,需要传入 client_id 和 client_secret

  • refresh_token 刷新令牌

  • authorization_code 授权码模式

  • urn:ietf:params:oauth:grant-type:device_code 设备流模式

  • password 帐密模式

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 范围,非必须,支持多选,多个取值以空格分隔。 可选取值:

  • openid

  • email

  • phone

  • profile

xxxx

返回参数

名称

类型

描述

示例值

object

响应结果。

token_type

string

token 类型,取值可选范围: Basic - Basic 类型 Bearer - Bearer 类型

枚举值:

  • Basic :

    Basic

  • Bearer :

    Bearer

Bearer

access_token

string

access_token。

ATxxx

refresh_token

string

refresh_token。

RTxxx

expires_in

integer

有效时长,单位秒。

1200

expires_at

integer

失效时间,格式为 Unix 时间戳,单位为秒。

1653288641

id_token

string

id_token。

xxxxx

示例

正常返回示例

JSON格式

{
  "token_type": "Bearer",
  "access_token": "ATxxx",
  "refresh_token": "RTxxx",
  "expires_in": 1200,
  "expires_at": 1653288641,
  "id_token": "xxxxx"
}

错误码

访问错误中心查看更多错误码。

变更历史

更多信息,参考变更详情