Initializing the API Client

Sample Code

To use CS SDK to access CS, you need to initialize the CS API client. During CS client initialization, you can perform authentication using the AK/SK or token. The sample code is as follows:
1
2
3
4
5
6
7
8
//Create an API client in a specified region.
ApiClient apiClient = new ApiClient("regionName");
//Use token authentication.
apiClient.useToken("accountName", "userName", "password", "projectId");
//Use AK/SK authentication.
apiClient.useAksk("ak", "sk");
//Use AK/SK authentication. You must specify projectId if you use AK/SK authentication for a customized project.
apiClient.useAksk("ak", "sk", "projectId");
  • AK/SK authentication is supported in 1.1.1 and later versions.
  • In the example, regionName is the name of the region to which the service belongs. For example, regionName for the CN North-Beijing1 region is cn-north-1. For details about other region names, see Regions and Endpoints.
  • accountName, userName, and password indicate the account name, username, and password of a HUAWEI CLOUD user, respectively. You can log in to the management console and obtain the information from the My Credentials page.
  • projectId indicates the project ID. For details about how to obtain the project ID, see How Do I Obtain the Project ID?.
  • For details about how to obtain the AK/SK, see How Do I Obtain the AK/SK?.
  • If you use AK/SK authentication for a customized project, you must specify projectId.