This topic describes how to call an API operation in OpenAPI Explorer to obtain and use the sample code of Server Migration Center (SMC) SDK for Java.
Prerequisites
The version of the Java Development Kit (JDK) is 1.8 or later and Java environment variables are configured.
A Java developer tool is downloaded and installed.
In this example, IntelliJ IDEA is used.
An AccessKey pair is obtained.
When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create and obtain an AccessKey pair, see Create an AccessKey pair.
Procedure
Step 1: Download the complete SDK project
Go to the API Debugging page of OpenAPI Explorer. In the left-side navigation pane, click the API operation that you want to call.
In this example, the DescribeSourceServers operation is used.
In the DescribeSourceServers pane, select a region and configure parameters on the Parameters tab. You can see API reference to view information related to the API, such as the description, usage notes, and expenses of the API operations and the specific meaning and usage of each parameter.
Click the SDK Sample Code tab on the right. Select Java as Languages and click Download Project.
NoteFor more information about how to copy the SDK sample code, see Automatic generation of SDK examples.
For more information about the sample code of SMC SDK for Java, see Quick Start in OpenAPI Explorer.
Step 2: Import the decompressed project
Open IntelliJ IDEA.
In the top navigation bar, select
.In the dialog box that appears, select the decompressed SDK project file.
Step 3: Configure credentials
Configure the credentials that are required when you call the API operations of Alibaba Cloud services. You can define the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID
and ALIBABA_CLOUD_ACCESS_KEY_SECRET
to configure the default credentials. When you call an API operation, the system reads the AccessKey pair from the default credentials and uses the AccessKey pair to complete authentication.
You can use multiple configuration methods to configure credentials. For more information, see Configure credentials.
In the
pom.xml
file, add the following Maven dependency to install the Alibaba Cloud SDK Credentials package.NoteThe Alibaba Cloud SDK Credentials package is continuously updated. For information about the latest version, see ChangeLog at Github.
<dependency> <groupId>com.aliyun</groupId> <artifactId>credentials-java</artifactId> <version>0.2.11</version> </dependency>
Right-click the pom.xml file and choose Maven > Reload project. Then, Maven automatically downloads required JAR packages for the pom.xml file.
Configure the environment variables
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
.NoteFor more information about how to create and obtain an AccessKey pair, see Create an AccessKey pair.
Linux and macOS
Open the terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>
with your AccessKey ID and<access_key_secret>
with your AccessKey secret in the following command.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Windows
Create an environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
to the file, and then specify your AccessKey ID for ALIBABA_CLOUD_ACCESS_KEY_ID and your AccessKey secret for ALIBABA_CLOUD_ACCESS_KEY_SECRET. Restart the Windows operating system. In this example, Windows 10 is used.Open the File Explorer. Right-click This PC and click Properties.
In the left-side navigation pane, click Advanced System Settings.
In the System Properties dialog box, click the Advanced tab and click Environment Variables.
In the Environment Variables dialog box, click New....
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_ID
andALIBABA_CLOUD_ACCESS_KEY_SECRET
. Then, enter the AccessKey ID and AccessKey secret.Restart the Windows operating system.
Step 4: Run the SDK sample code
In the left-side navigation pane of IntelliJ IDEA, open the SDK for Java sample code file.
In the top navigation bar, select
to run the sample code.Check the result of the code.
You can configure an HTTPS request, a proxy, a timeout period, or a retry mechanism for the SDK based on your business requirements. For more information, see Advanced configuration.