腾讯云开发者工具套件(SDK)是云 API 3.0 平台的配套工具。本文以 Java SDK 为例,介绍如何使用、调试并接入腾讯云产品 API。
要使用腾讯云 Java SDK,用户需要满足以下的依赖环境:
前往 官网安装指南界面 ,按照指引下载并安装JDK8
前往 IEDA官方安装指南界面 ,按照指引下载并安装IDEA
操作步骤如下(以IDEA为例示范安装):
为您的项目添加 Maven 依赖项,只需在 pom.xml 中找到或手动输入dependencies标签,在里面添加以下依赖项即可。
<dependencies>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<!-- go to https://search.maven.org/search?q=tencentcloud-sdk-java and get the latest version. -->
<!-- 请到https://search.maven.org/search?q=tencentcloud-sdk-java查询所有版本,当前最新版本如下 -->
<version>3.1.302</version>
</dependency>
</dependencies>
添加依赖项代码示意图如下所示:
查看项目目录下的外部库中是否有下图框出来的文件,若存在这些文件则表明已经成功安装SDK。
安装流程如下:
前往 API 密钥管理 页面获取安全凭证 SecretId 和 SecretKey 两部分,在上文的依赖环境中已提到该点。
我们为您提供简化版和详细版两个版本的示例代码,您可以任选一个版本进行测试使用,具体详细的使用流程在下文中会有说明,示例代码如下所示:
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.cvm.v20170312.CvmClient;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;
public class DescribeInstances {
public static void main(String[] args) {
try {
Credential cred = new Credential("secretId", "secretKey");
CvmClient client = new CvmClient(cred, "ap-shanghai");
DescribeInstancesRequest req = new DescribeInstancesRequest();
DescribeInstancesResponse resp = client.DescribeInstances(req);
System.out.println(DescribeInstancesResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
// 导入对应产品模块的client
import com.tencentcloudapi.cvm.v20170312.CvmClient;
// 导入要请求接口对应的request response类
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;
import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;
import com.tencentcloudapi.cvm.v20170312.models.Filter;
//导入可选配置类
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.Language;
public class DescribeInstances {
public static void main(String[] args) {
try {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
Credential cred = new Credential("secretId", "secretKey");
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
// 从3.1.16版本开始, 单独设置 HTTP 代理
// httpProfile.setProxyHost("真实代理ip");
// httpProfile.setProxyPort(真实代理端口);
httpProfile.setReqMethod("GET"); // get请求(默认为post请求)
httpProfile.setProtocol("https://"); // 在外网互通的网络环境下支持http协议(默认是https协议),请选择(https:// or http://)
httpProfile.setConnTimeout(30); // 请求连接超时时间,单位为秒(默认60秒)
httpProfile.setWriteTimeout(30); // 设置写入超时时间,单位为秒(默认0秒)
httpProfile.setReadTimeout(30); // 设置读取超时时间,单位为秒(默认0秒)
httpProfile.setEndpoint("cvm.ap-shanghai.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod("HmacSHA256"); // 指定签名算法(默认为HmacSHA256)
// 自3.1.80版本开始,SDK 支持打印日志。
clientProfile.setHttpProfile(httpProfile);
clientProfile.setDebug(true);
// 从3.1.16版本开始,支持设置公共参数 Language, 默认不传,选择(ZH_CN or EN_US)
clientProfile.setLanguage(Language.EN_US);
// 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的
CvmClient client = new CvmClient(cred, "ap-shanghai", clientProfile);
// 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
DescribeInstancesRequest req = new DescribeInstancesRequest();
// 填充请求参数,这里request对象的成员变量即对应接口的入参
// 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
Filter respFilter = new Filter(); // 创建Filter对象, 以zone的维度来查询cvm实例
respFilter.setName("zone");
respFilter.setValues(new String[] { "ap-shanghai-1", "ap-shanghai-2" });
req.setFilters(new Filter[] { respFilter }); // Filters 是成员为Filter对象的列表
// 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
// 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
DescribeInstancesResponse resp = client.DescribeInstances(req);
// 输出json格式的字符串回包
System.out.println(DescribeInstancesResponse.toJsonString(resp));
// 也可以取出单个值。
// 你可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
System.out.println(resp.getTotalCount());
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
}
使用简化版示例代码调用 API 的流程如下(详细版同理):
当"secretId", "secretKey"未用您真实的密钥进行替换时会报错,如下图所示。更多错误码内容请阅览产品文档错误码章节。
您可以在github中examples目录下找到更多详细的示例。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。