This topic describes how to use Alibaba Cloud SDKs to call OpenAPI.
Alibaba Cloud SDKs
Alibaba Cloud provides software development kits (SDKs) for developers in multiple programming languages, such as Java, C#, Go, Python, Node.js/TypeScript, PHP, and C++. You can integrate an SDK to directly call OpenAPI using the methods that the SDK exposes. The SDKs encapsulate the signature logic, timeout mechanism, and retry mechanism. They also return structured response objects based on the documentation to simplify your development process.
For more information about Alibaba Cloud SDKs, see Alibaba Cloud SDKs.
Integration method
Visit the OpenAPI Portal, click the
button at the top, and search for cloud products.Click the SDK menu at the top and select your preferred language for integration.
View the SDK installation method and code examples to complete the integration.
Comparison of V2 and V1
The upgraded SDK (V2) supports more languages and more complex OpenAPI scenarios than the original SDK (V1). V2 supports both asynchronous and synchronous calls and resolves some legacy issues from V1, which makes it more flexible and powerful. We recommend that you use the upgraded SDK (V2). For more information, see V2.0 SDK and V1.0 SDK.
For new projects, use the upgraded SDK (V2). If your project already uses the original SDK (V1), we recommend that you upgrade it.
Integration example
This section provides an example of how to integrate the Java SDK to call the DescribeInstance operation of Elastic Compute Service (ECS).
V2
The upgraded SDK (V2) contains the main logic for OpenAPI requests, such as parameter processing, request assembly, and return value handling. You can make OpenAPI requests by installing the dependency package for a product's SDK. You no longer need to rely on the core library.
Dependency
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>
<version>5.1.0</version>
</dependency>Sample code
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize the client.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Your AccessKey ID.
.setAccessKeyId(accessKeyId)
// Required. Your AccessKey secret.
.setAccessKeySecret(accessKeySecret);
// The endpoint to access.
config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
return new com.aliyun.ecs20140526.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// Ensure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are set.
// Leaking your project code can expose your AccessKey pair, which compromises the security of all resources in your account. The following code shows how to get an AccessKey pair from environment variables for making calls. This method is for reference only. Use a more secure method, such as Security Token Service (STS). For more information about authentication, see the topic about authentication configuration for Alibaba Cloud SDK for Java.
com.aliyun.ecs20140526.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// If you copy and run this code, add your own code to print the API return value.
client.runInstancesWithOptions(runInstancesRequest, runtime);
} catch (TeaException error) {
// If an error occurs, print the error message as needed.
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// If an error occurs, print the error message as needed.
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
V1
Install dependencies
Install the SDK core library. The core library contains the main logic for OpenAPI, such as handling HTTP requests, authentication, signature algorithms, and exceptions.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-ecs</artifactId>
<version>5.11.5</version>
</dependency>Sample code
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
public class DescribeInstances {
public static void main(String[] args) {
// Create and initialize a DefaultAcsClient instance.
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
IAcsClient client = new DefaultAcsClient(profile);
// Create an API request and set the parameters.
DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setRegionId("cn-hangzhou");
request.setInstanceNetworkType("vpc");
request.setInstanceChargeType("PostPaid");
request.setInternetChargeType("PayByTraffic");
request.setPageSize(10);
try {
// Initiate the request and handle the response or exceptions.
DescribeInstancesResponse response = client.getAcsResponse(request);
for (DescribeInstancesResponse.Instance instance : response.getInstances()) {
System.out.println(instance.getImageId());
System.out.println(instance.getInstanceId());
System.out.println(instance.getPublicIpAddress());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}