Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Elastic Cloud Server/ API Reference/ Application Examples/ Obtaining a Token and Checking the Validity Period of the Token

Obtaining a Token and Checking the Validity Period of the Token

Updated on 2024-04-15 GMT+08:00

Scenarios

The validity period of a token is 24 hours. After obtaining a token, store it to prevent frequent API calling. The original token will remain valid until it expires regardless of whether a new token has been obtained. Ensure that the token is valid when you use it. Using a token that will soon expire may cause API calling failures.

This section describes how to obtain a token and check its validity period to resolve the API calling failures caused by token expiration.

If the token is about to expire (cannot complete an API call or a set of API calls), you need to obtain a new token to prevent the call from being interrupted due to token expiration.

Obtaining a Token

You need to obtain a token for authentication before calling an API. The X-Auth-Token value in the request header is the token value.

The following is an example to show how to obtain the token of the CN-Hong Kong region.

  • Request URI: POST https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
  • Request header: Content-Type=application/json
  • Request message body:
    {
        "auth": {
            "identity": {
                "methods": [
                    "password"
                ],
                "password": {
                    "user": {
                        "name": "Enter a username",
                        "password": "$ADMIN_PASS",     //You are advised to store it in ciphertext in the configuration file or an environment variable and decrypt it when needed to ensure security.
                        "domain": {
                            "name": "Enter the account name."
                        }
                    }
                }
            },
            "scope": {
                "project": {
                    "name": "Enter a region name. In this example, the region is ap-southeast-1."
                }
            }
        }
    }

  • To view the obtained token, click the response header. The value of x-subject-token is the obtained token. Keep the token secure because it will be used in the request header for creating ECSs.
    General:
       Request URL: https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
       Request Method: POST
       Status Code: 201
    Response Headers:
       cache-control: no-cache, no-store, must-revalidate
       connection: keep-alive
       content-length: 18401
       content-type: application/json; charset=UTF-8
       date: Thu, 27 May 2021 01:24:49 GMT
       expires: Thu, 01 Jan 1970 00:00:00 GMT
       pragma: no-cache
       server: api-gateway
       strict-transport-security: max-age=31536000; includeSubdomains;
       via: proxy A
       x-content-type-options: nosniff
       x-download-options: noopen
       x-frame-options: SAMEORIGIN
       x-iam-trace-id: token_ap-southeast-1_null_9bbec3983f3c7a5c146e709251760467
       x-request-id: d7796611318416bc8ffb2948a47fede8
       x-subject-token: MIISMAYJKoZIhvcNAQ...7xMUw==
       x-xss-protection: 1; mode=block;
  • The expires_at value in the response body is the token expiration time.
    {
    	"token": {
    		"expires_at": "2021-05-28T01:24:49.905000Z",
            ...
    	}
    }

Checking the Token Validity Period

When making an API call, the system checks whether the validity period of the token is long enough. If your application has cached the token, it is recommended that the token be refreshed every 12 hours to ensure that it has a long enough validity period.

You can query the expiration time of a token by referring to Verifying a Token.

The following is an example to show how to verify the token of the CN-Hong Kong region.

  • Request URI: GET https://iam.ap-southeast-1.myhuaweicloud.com/v3/auth/tokens
  • Request header:
    • Content-Type=application/json;charset=utf8
    • X-Auth-Token: A token with Security Administrator permissions is required if the administrator is requesting to verify the token of an IAM user.

      The user token (no special permission requirements) of an IAM user is required if the user is requesting to verify their own token.

      This example uses the IAM user and the X-Auth-Token is the same as the token to be verified

    • X-Subject-Token: Token to be verified.
  • The expires_at value in the response body is the token expiration time.

    If the token is about to expire (cannot complete an API call or a set of API calls), you need to obtain a new token to prevent the call from being interrupted due to token expiration.

    {
    	"token": {
    		"expires_at": "2021-05-28T01:24:49.905000Z",
            ...
    	}
    }

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback