Obtaining a User Token Through Password Authentication
Function
This API is used to obtain a user token through username/password-based authentication. A token is an access credential issued to an IAM user to bear its identity and permissions. When calling the APIs of IAM or other cloud services, you can use this API to obtain a user token for authentication.
The API can be called using both the global endpoint and region-specific endpoints. For IAM endpoints, see Regions and Endpoints.
- Validity period of a token
The validity period of a token is 24 hours. Cache the token to prevent frequent API calling. Ensure that the token is valid while you use it. Using a token that will soon expire may cause API calling failures.
Obtaining a new token does not affect the validity of the existing token. However, the following operations will invalidate the existing token:
- Deleting or disabling the IAM user
- Changing the IAM user's password or access key
- The IAM user's permissions are changed due to outstanding payments, OBT application approval, or permission modification.
- Obtaining a token
- If your HUAWEI CLOUD account has been upgraded to a HUAWEI ID, you cannot obtain a token using the HUAWEI ID. Instead, you can create an IAM user, grant the user required permissions, and obtain a token as the user.
- If you are a user of a third-party system, you cannot obtain a token by using the username and password that you use for federated identity authentication. Go to the HUAWEI CLOUD login page, click Forgot password, click Reset HUAWEI CLOUD account password, and set a password.
- Related operations
- If login protection has been enabled and the verification method has been set to virtual MFA device, obtain a token as an IAM user by following the instructions provided in Obtaining a User Token Through Password and Virtual MFA Authentication.
- To obtain a token with Security Administrator permissions, see How Do I Obtain a Token with Security Administrator Permissions?
- For details on how to obtain a token using Postman, see How Do I Obtain a User Token Using Postman?
URI
POST /v3/auth/tokens
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
nocatalog |
No |
String |
If this parameter is set, no catalog information will be displayed in the response. Any character string set for this parameter indicates that no catalog information will be displayed. |
Request Parameters
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
Content-Type |
Yes |
String |
Fill application/json;charset=utf8 in this field. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
Yes |
Object |
Authentication information. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
Yes |
Object |
Authentication parameters. |
|
|
Yes |
Object |
Application scope of the token. The value can be project or domain.
NOTE:
|
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
methods |
Yes |
Array of strings |
Authentication method. The content of this field is ["password"]. |
|
Yes |
Object |
Password authentication information of an IAM user.
NOTE:
|
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
Yes |
Object |
Information about the IAM user who is requesting to obtain a token. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
Yes |
Object |
Information about the account used to create the IAM user. For details, see Relationship Between an Account and Its IAM Users |
|
|
name |
Yes |
String |
IAM user name. |
|
password |
Yes |
String |
Password of the IAM user.
NOTE:
|
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
name |
Yes |
String |
Account name. For details about how to obtain the account name, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
No |
Object |
If this field is set to domain, the token can be used to access global services, such as OBS. Global services are not subject to any projects or regions. For details about the service scope, see System Permissions. You can specify either id or name. domain_id is recommended. |
|
|
No |
Object |
If this field is set to project, the token can be used to access only services in specific projects, such as ECS. For details about the service scope, see System Permissions. You can specify either id or name. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
id |
No |
String |
Account ID. For details about how to obtain the account ID, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information. You can specify either id or name. |
|
name |
No |
String |
Account name. For details about how to obtain the account name, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information. You can specify either id or name. |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
id |
No |
String |
Project ID. For details about how to obtain the project ID, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information. You can specify either id or name. |
|
name |
No |
String |
Project name. For details about how to obtain the project name, see Obtaining Account, IAM User, Group, Project, Region, and Agency Information. You can specify either id or name. |
Example Request
- Request for obtaining a token for IAM user IAMUser (password: IAMPassword; account name: IAMDomain; scope: project cn-north-1) without displaying catalog information in the response
POST https://iam.myhuaweicloud.com/v3/auth/tokens?nocatalog=true
{ "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "domain": { "name": "IAMDomain" // Name of the account to which the IAM user belongs. }, "name": "IAMUser", // IAM user name. "password": "IAMPassword" // IAM user password. } } }, "scope": { "project": { "name": "cn-north-1" // Project name. } } } } - Request for obtaining a token for IAM user IAMUser (password: IAMPassword; account name: IAMDomain; scope: domain)
POST https://iam.myhuaweicloud.com/v3/auth/tokens
{ "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "domain": { "name": "IAMDomain" // Name of the account to which the IAM user belongs. }, "name": "IAMUser", // IAM user name. "password": "IAMPassword" // IAM user password. } } }, "scope": { "domain": { "name": "IAMDomain" // Name of the account to which the IAM user belongs. } } } }
Response Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
X-Subject-Token |
String |
Signed token. |
|
Parameter |
Type |
Description |
|---|---|---|
|
Object |
Token information. |
|
Parameter |
Type |
Description |
|---|---|---|
|
Array of objects |
Catalog information. |
|
|
Object |
Account information of the IAM user who requests for the token. This parameter is returned only when the scope parameter in the request body has been set to domain. |
|
|
expires_at |
String |
Time when the token will expire. |
|
issued_at |
String |
Time when the token was issued. |
|
methods |
Array of strings |
Method for obtaining the token. |
|
Object |
Project information of the IAM user. This parameter is returned only when the scope parameter in the request body has been set to project. |
|
|
Array of objects |
Permissions information of the token. |
|
|
Object |
Information about the IAM user who requests for the token. |
|
Parameter |
Type |
Description |
|---|---|---|
|
Array of objects |
Endpoint information. |
|
|
id |
String |
Service ID. |
|
name |
String |
Service name. |
|
type |
String |
Type of the service to which the API belongs. |
|
Parameter |
Type |
Description |
|---|---|---|
|
id |
String |
Endpoint ID. |
|
interface |
String |
Visibility of the API. public indicates that the API is available for public access. |
|
region |
String |
Region to which the endpoint belongs. |
|
region_id |
String |
Region ID. |
|
url |
String |
Endpoint URL. |
|
Parameter |
Type |
Description |
|---|---|---|
|
Object |
Account information of the project. |
|
|
id |
String |
Project ID. |
|
name |
String |
Project name. |
|
Parameter |
Type |
Description |
|---|---|---|
|
id |
String |
Account ID. |
|
name |
String |
Account name. |
|
Parameter |
Type |
Description |
|---|---|---|
|
name |
String |
Permission name. |
|
id |
String |
Permission ID. The default value is 0, which does not correspond to any permission. |
|
Parameter |
Type |
Description |
|---|---|---|
|
name |
String |
IAM user name. |
|
id |
String |
IAM user ID. |
|
password_expires_at |
String |
UTC time when the password will expire. If this parameter is empty, it indicates that the password has unlimited validity. |
|
Object |
Information about the account used to create the IAM user. |
Example Response
Status code: 201
The request is successful.
- Response to the request for obtaining a token for IAM user IAMUser (password: IAMPassword; account name: IAMDomain; scope: project cn-north-1) without displaying catalog information in the response
Parameters in the response header (obtained token) X-Subject-Token:MIIatAYJKoZIhvcNAQcCoIIapTCCGqECAQExDTALB...
Parameters in the response body { "token": { "catalog": [], "expires_at": "2020-01-04T09:05:22.701000Z", "issued_at": "2020-01-03T09:05:22.701000Z", "methods": [ "password" ], "project": { "domain": { "id": "d78cbac186b744899480f25bd022f...", "name": "IAMDomain" }, "id": "aa2d97d7e62c4b7da3ffdfc11551f...", "name": "cn-north-1" }, "roles": [ { "id": "0", "name": "te_admin" }, { "id": "0", "name": "op_gated_OBS_file_protocol" }, { "id": "0", "name": "op_gated_Video_Campus" } ], "user": { "domain": { "id": "d78cbac186b744899480f25bd022f...", "name": "IAMDomain" }, "id": "7116d09f88fa41908676fdd4b039e...", "name": "IAMUser", "password_expires_at": "" } } } - Response to the request for obtaining a token for IAM user IAMUser (password: IAMPassword; account name: IAMDomain; scope: domain)
Parameters in the response header (obtained token) X-Subject-Token:MIIatAYJKoZIhvcNAQcCoIIapTCCGqECAQExDTALB...
Parameters in the response body { "token": { "catalog": [ { "endpoints": [ { "id": "33e1cbdd86d34e89a63cf8ad16a5f...", "interface": "public", "region": "*", "region_id": "*", "url": "https://iam.myhuaweicloud.com/v3.0" } ], "id": "100a6a3477f1495286579b819d399...", "name": "iam", "type": "iam" }, { "endpoints": [ { "id": "29319cf2052d4e94bcf438b55d143...", "interface": "public", "region": "*", "region_id": "*", "url": "https://bss.myhuaweicloud.com/v1.0" } ], "id": "c6db69fabbd549908adcb861c7e47...", "name": "bssv1", "type": "bssv1" } ], "domain": { "id": "d78cbac186b744899480f25bd022f...", "name": "IAMDomain" }, "expires_at": "2020-01-04T09:08:49.965000Z", "issued_at": "2020-01-03T09:08:49.965000Z", "methods": [ "password" ], "roles": [ { "id": "0", "name": "te_admin" }, { "id": "0", "name": "secu_admin" }, { "id": "0", "name": "te_agency" } ], "user": { "domain": { "id": "d78cbac186b744899480f25bd022f...", "name": "IAMDomain" }, "id": "7116d09f88fa41908676fdd4b039e...", "name": "IAMUser", "password_expires_at": "" } } }
Status code: 400
Invalid parameters.
{
"error": {
"code": 400,
"message": "The request body is invalid",
"title": "Bad Request"
}
}
Status code: 401
Authentication failed.
- If you are a user of a third-party system, you cannot obtain a token by using the username and password that you use for federated identity authentication. Go to the HUAWEI CLOUD login page, click Forgot password, click Reset HUAWEI CLOUD account password, and set a new password.
- If your HUAWEI CLOUD account has been upgraded to a HUAWEI ID, you cannot obtain a token using the HUAWEI ID. Instead, you can create an IAM user, grant the user required permissions, and obtain a token as the user.
{
"error": {
"code": 401,
"message": "The username or password is wrong.",
"title": "Unauthorized"
}
}
Status Codes
|
Status Code |
Description |
|---|---|
|
201 |
The request is successful. |
|
400 |
Invalid parameters. |
|
401 |
Authentication failed. |
|
403 |
Access denied. |
|
404 |
The requested resource cannot be found. |
|
500 |
Internal server error. |
|
503 |
Service unavailable. |
Error Codes
None
Last Article: Token Management
Next Article: Obtaining a User Token Through Password and Virtual MFA Authentication
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.