
# 创建令牌 - CreateToken
#### 功能介绍
获取访问令牌。
#### URI
POST /v1/tokens
#### 请求参数
表1请求Body参数 
| 参数            | 是否必选 | 参数类型             | 描述                                                                                                                                                                                                                                                                                                                                                      |
|:---|:---|:---|:---|
| client_id     | 是    | String           | 客户端的唯一标识。                                                                                                                                                                                                                                                                                                                                               |
| client_secret | 是    | String           | 为客户端生成的秘密字符串。客户端将使用此字符串在后续调用中获得服务的身份验证。                                                                                                                                                                                                                                                                                                                 |
| code          | 否    | String           | 从授权服务接收的授权代码。执行授权授予请求以获取对令牌的访问权限时需要此参数。                                                                                                                                                                                                                                                                                                                 |
| device_code   | 否    | String           | 仅在为设备代码授权类型调用此API时使用                                                                                                                                                                                                                                                                                                                                    |
| grant_type    | 是    | String           | 请求的授权类型。支持授权码、设备代码、客户端凭证和刷新令牌等授权类型。 枚举值： - **authorization_code**：授权码。  - **urn:ietf:params:oauth:grant-type:device_code**：设备码。   |
| redirect_uri  | 否    | String           | 将接收授权代码的应用程序的位置。用户授权服务将请求发送到此位置。                                                                                                                                                                                                                                                                                                                        |
| refresh_token | 否    | String           | 刷新令牌，此令牌可在访问令牌过期后获取新的访问令牌。                                                                                                                                                                                                                                                                                                                              |
| scopes        | 否    | Array of strings | 客户端定义的作用域列表，表示客户端想要获取的权限。授权后，此列表用于在授予访问令牌时限制权限。                                                                                                                                                                                                                                                                                                         |
   
#### 响应参数
**状态码： 200**
表2响应Body参数 
| 参数                                                                             | 参数类型   | 描述         |
|:---|:---|:---|
| [token_info] | Object | 包含令牌信息的对象。 |
   
 表3token_info 
| 参数            | 参数类型    | 描述                                |
|:---|:---|:---|
| access_token  | String  | 用于访问分配给用户的IAM身份中心资源的不透明令牌。        |
| expires_in    | Integer | 访问令牌的过期时间（以秒为单位）。                 |
| id_token      | String  | 用于表明用户身份的不透明令牌。                   |
| refresh_token | String  | 刷新令牌，此令牌可在访问令牌过期后获取新的访问令牌。        |
| token_type    | String  | 用于通知客户端返回的令牌是访问令牌，目前为BearerToken。 |
   
#### 请求示例
创建访问令牌。
```
POST https://{hostname}/v1/tokens
{
  "client_id" : "example_client_id",
  "client_secret" : "example_client_secret",
  "code" : "1234567890123456",
  "device_code" : null,
  "grant_type" : "authorization_code",
  "redirect_uri" : "https://example-redirect.example.com/redirect/url",
  "refresh_token" : null,
  "scopes" : [ "openid" ]
}
```
#### 响应示例
**状态码： 200**
Successful
```
{
  "token_info" : {
    "access_token" : "example_access_token",
    "expires_in" : 1684955360,
    "id_token" : "example_access_token",
    "refresh_token" : null,
    "token_type" : "Bearer"
  }
}
```
#### 状态码
请参见[状态码](https://support.huaweicloud.com/api-identitycenter/iic_04_0030.html)。
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-identitycenter/ErrorCode.html)。
