Java SDK Authentication Modes

Java SDK supports two authentication modes: token-based authentication and AK/SK authentication.

Token Authentication

For details about the code for token-based authentication, see Table 1.

package demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huawei.openstack4j.openstack.OSFactory;
import com.huawei.openstack4j.api.OSClient.OSClientV3;
import com.huawei.openstack4j.core.transport.Config;
import com.huawei.openstack4j.model.common.Identifier;
import com.huawei.openstack4j.model.compute.Server;

public class Demo {
    public static void main(String[] args) {
       // Set the authentication parameters.
        String authUrl = "https://iam.example.com/v3";//endpointUrl
        String user = "replace-with-your-username";//username
        String password = "replace-with-your-password";//user password
        String projectId = "replace-with-your-projectId";//project ID
        String userDomainId = "replace-with-your-domainId";//account ID

       // Initialize the client.
        OSClientV3 os = OSFactory.builderV3()
                .endpoint(authUrl)
                .credentials(user, password, Identifier.byId(userDomainId))
                .scopeToProject(Identifier.byId(projectId)).authenticate();

       // Set query parameters.
        Map<String, String> filter = new HashMap<String, String>();

       // Put the parameters that need to be entered into the filter.
        filter.put("limit", "3");

       // Invoke the interface for querying the VM List.
        List<? extends Server> serverList = os.compute().servers().list(filter);
        if (serverList.size() > 0) {
            System.out.println("get serverList success, size = " + serverList.size());
            for (Server server : serverList) {
                System.out.println(server);
            }
        } else {
            System.out.println("no server exists.");
        }
    }
}
Table 1 Parameter description

Parameter

Description

Example Value

authUrl

Specifies the endpoint of the IAM service.

example in https://iam.example.com/v3 indicates Region.Cloud platform domain name. For details about the parameter, see here.

https://iam.cn-north-1.myhuaweicloud.com/v3

user

Specifies the IAM username. For details about how to obtain the username, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

password

Specifies the IAM user password.

N/A

projectId

Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

None

userDomainId

Specifies the account ID. For details about how to obtain the account ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

None

The validity period of a token is 24 hours. If your operation time exceeds 24 hours, before you use SDK to invoke APIs, you are advised to reapply the token according to the following method:

import com.huawei.openstack4j.openstack.OSFactory;
OSFactory.refreshToken();

AK/SK Authentication

For details about the code for AK/SK authentication, see Table 2.

package demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huawei.openstack4j.api.OSClient.OSClientAKSK;
import com.huawei.openstack4j.core.transport.Config;
import com.huawei.openstack4j.model.compute.Server;
import com.huawei.openstack4j.openstack.OSFactory;

public class Demo {
 
 public static void main(String[] args) {
 
  // Set the authentication parameters.
  String ak = "replace-your-ak";
  String sk = "replace-your-sk";
  String projectId = "replace-your-projectId";
  String region = "replace-your-region"; //example: region = "cn-north-1"
  String cloud = "myhuaweicloud.com";
 
  OSClientAKSK osclient = OSFactory.builderAKSK().credentials(ak, sk, region, projectId, cloud) .authenticate();
 
  // Set query parameters.
  Map<String , String> filter = new HashMap<String, String>();
  // Put the parameters that need to be entered into the filter.
  filter.put("limit", "3");
  
  // Invoke the interface for querying the VM List.
  List<? extends Server> serverList = osclient.compute().servers().list(filter);
  if(serverList.size() > 0) 
  {
   System.out.println("get serverList success, size = " + serverList.size());
   for (Server server : serverList) {
    System.out.println(server);
   }
  } 
  else {
   System.out.println("no server exists.");
  }
 }
}
Table 2 Parameter description

Parameter

Description

Example Value

ak/sk

Specifies the AK/SK access key.

NOTE:
  • AK/SK generation description: Log in to the management console, choose My Credentials, and click Access Keys to create an AK and SK.
  • The time error between the AK/SK signature time and UTC time cannot exceed 15 minutes. Otherwise, the authentication fails.
  • If the AK/SK signature fails for more than five consecutive times, the AK/SK request of the source IP address is locked for 5 minutes.

N/A

projectId

Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

region

Specifies the region name.

cn-north-1

cloud

Specifies the cloud platform domain name.

myhuaweicloud.com