Updated on 2025-08-21 GMT+08:00

Authentication

You can use either of the following authentication methods when calling APIs:

  • Token-based authentication. General requests are authenticated using tokens.
  • AK/SK-based authentication: Requests are encrypted using an Access Key ID/Secret Access Key (AK/SK) pair.

Token-based Authentication

  • The validity period of a token is 24 hours. When using a token for authentication, cache it to prevent frequently calling the API for obtaining a user token.
  • Ensure that the token is valid when you use it. Using a token that will soon expire may cause API calling failures.

A token specifies temporary permissions in a computer system. During API authentication using a token, the token is included in the request headers to get permissions for calling the API.

A project-level token is required for calling CodeArts Build APIs. When calling an API to obtain a user token, set auth.scope in the request body to project, as shown in the following example.

{ 
     "auth": { 
         "identity": { 
             "methods": [ 
                 "password" 
             ], 
             "password": { 
                 "user": { 
                     "name": "username", 
                     "password": "********", 
                     "domain": { 
                         "name": "domainname" 
                     } 
                 } 
             } 
         }, 
         "scope": {
             "project": {
                 "name": "xxxxxxxx"
             }
         }
     } 
 }

After a token is obtained, the X-Auth-Token header field must be added to requests to specify the token when other APIs are called. For example, if the token is ABCDEFJ...., X-Auth-Token: ABCDEFJ.... can be added to a request as follows:

GET https://iam.ap-southeast-3.myhuaweicloud.com/v3.0/OS-USER/users
Content-Type: application/json
X-Auth-Token: ABCDEFJ....

AK/SK-based Authentication

You can use AK/SK to verify the identity of a request sender. In AK/SK-based authentication, a signature needs to be obtained through the request signing process and then added to the request headers.

AK: access key ID, which is a unique identifier used in conjunction with a secret access key to sign requests cryptographically.

SK: secret access key used in conjunction with an AK to sign requests cryptographically. It identifies a request sender and prevents the request from being modified.

The following uses a demo project to show how to sign a request and use an HTTP client to send an HTTPS request.

Download the demo from https://github.com/api-gate-way/SdkDemo.

If you do not need the demo project, download the API Gateway signing tool and reference it in other projects.

Decompress the downloaded package to obtain a JAR file. Reference the obtained JAR file as a dependency, as shown in the following figure.

  1. Create an AK/SK pair. If an AK/SK pair has already been generated, skip this step. Find the downloaded AK/SK file, which is usually named credentials.csv.

    1. Log in to the management console.
    2. Hover over your username and select My Credentials from the drop-down list.
    3. In the navigation pane, click Access Keys.
    4. Click Create Access Key.
    5. On the displayed page, enter your login password.
    6. Enter the verification code sent to your email or mobile phone.

      For users created in IAM, if no email address or phone number was specified during the user creation, only a login password is required.

    7. Click OK to download the access key.

      Keep the access key secure.

  2. Obtain and decompress the demo project.
  3. Import the demo project to Eclipse.

    Figure 1 Selecting an existing project
    Figure 2 Selecting the demo project
    Figure 3 Example structure of the demo project

  4. Sign the request.

    The signature method is integrated into the JAR file imported in step 3. Sign a request before sending it. The signature will be included in the HTTP header of the request.

    The demo code is classified into the following classes for demonstration:

    • AccessService: It is an abstract class that integrates the GET, POST, PUT, and DELETE methods into the access method.
    • Demo: It is an execution entry used to simulate the sending of GET, POST, PUT, and DELETE requests.
    • AccessServiceImpl: It implements the access method, which contains the code required for communication with API Gateway.
    1. (Optional) Add request headers.
      Locate the following lines in the AccessServiceImpl.java file, uncomment them, and replace the subproject ID and account ID with the actual ones.
      //TODO: Add special headers. 
      //request.addHeader("X-Project-Id", "xxxxx"); 
      //request.addHeader("X-Domain-Id", "xxxxx");
    2. Edit the main method in the Demo.java file.

      Replace the bold texts with actual values. If you use other methods, such as POST, PUT, and DELETE, see the corresponding comments.

      Replace region, serviceName, AK/SK, and URL with the actual values. In the demo, the URL for obtaining the VPC is used. Replace it with the required URL. For details about how to obtain the project ID in the URL, see Obtaining an IAM Project ID. For details about how to obtain the endpoint, see Regions and Endpoints.

      //TODO: Replace region with the name of the region in which the service to be accessed is located.  
       private static final String region = ""; 
        
       //TODO: Replace vpc with the name of the service you want to access. For example, ecs, vpc, iam, and elb. 
       private static final String serviceName = ""; 
        
       public static void main(String[] args) throws UnsupportedEncodingException 
       { 
       //TODO: Replace the AK and SK with those obtained on the My Credential page. 
       String ak = "ZIRRKMTWP******1WKNKB"; 
       String sk = "Us0mdMNHk******YrRCnW0ecfzl"; 
        
       //TODO: To specify a project ID (multi-project scenarios), add the X-Project-Id header. 
       //TODO: To access a global service, such as IAM, DNS, CDN, and TMS, add the X-Domain-Id header to specify an account ID. 
       //TODO: To add a header, find "Add special headers" in the AccessServiceImple.java file. 
        
       //TODO: Test the API 
       String url = "https://{Endpoint}/v1/{project_id}/vpcs"; 
       get(ak, sk, url); 
        
       //TODO: When creating a VPC, replace {project_id} in postUrl with the actual value. 
       //String postUrl = "https://serviceEndpoint/v1/{project_id}/cloudservers"; 
       //String postbody ="{\"vpc\": {\"name\": \"vpc\",\"cidr\": \"192.168.0.0/16\"}}"; 
       //post(ak, sk, postUrl, postbody); 
        
       //TODO: When querying a VPC, replace {project_id} in url with the actual value. 
       //String url = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}"; 
       //get(ak, sk, url); 
        
       //TODO: When updating a VPC, replace {project_id} and {vpc_id} in putUrl with the actual values. 
       //String putUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}"; 
       //String putbody ="{\"vpc\":{\"name\": \"vpc1\",\"cidr\": \"192.168.0.0/16\"}}"; 
       //put(ak, sk, putUrl, putbody); 
        
       //TODO: When deleting a VPC, replace {project_id} and {vpc_id} in deleteUrl with the actual values. 
       //String deleteUrl = "https://serviceEndpoint/v1/{project_id}/vpcs/{vpc_id}"; 
       //delete(ak, sk, deleteUrl); 
       }
    3. Compile and run the code to call an API.

      In the Package Explorer area on the left, right-click Demo.java, and choose Run AS > Java Application to run the demo code.

      You can view the calling logs on the console.