SecretId and SecretKey. SecretId is used to identify the API requester, while SecretKey is a key used for signature string encryption and authentication by the server. You can get them on the API Key Management page as shown below:

*.tencentcloudapi.com and varies by service. For example, the endpoint of CVM is cvm.tencentcloudapi.com. For specific endpoints, please see the API documentation of the corresponding service.<dependencies> tag in Maven's pom.xml. You can find the latest version (v3.1.322) in the Maven repository.<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. --><!-- Please query the latest version at https://search.maven.org/search?q=tencentcloud-sdk-java, which is as follows --><version>3.1.322</version></dependency>
artifactId with a specific product SDK name such as tencentcloud-sdk-java-cvm/cbs/vpc to import the SDK of the specific product. The code can be used in the same way, and the major packages are the same. For more information, please see the samples. The latest version can also be queried in the Maven repository, which can greatly save the storage space.settings.xml configuration file of Maven and add the mirror configuration in the mirrors section:<mirror><id>tencent</id><name>tencent maven mirror</name><url>https://mirrors.tencent.com/nexus/repository/maven-public/</url><mirrorOf>*</mirrorOf></mirror>
vendor directory in a path that can be found by Java.DescribeInstances as an example: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;// Import the client of the corresponding product moduleimport com.tencentcloudapi.cvm.v20170312.CvmClient;// Import the request response class corresponding to the request APIimport com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest;import com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse;import com.tencentcloudapi.cvm.v20170312.models.Filter;// Import the optional configuration classesimport 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 {// Instantiate an authentication object. Pass in `secretId` and `secretKey` of your Tencent Cloud account as the input parameters and keep them confidentialCredential cred = new Credential("secretId", "secretKey");// (Optional) Instantiate an HTTP optionHttpProfile httpProfile = new HttpProfile();// Starting from v3.1.16, set up an HTTP proxy separately// httpProfile.setProxyHost("real proxy ip");// httpProfile.setProxyPort(real proxy port);httpProfile.setReqMethod("GET"); // GET request (POST request by default)httpProfile.setProtocol("https://"); // Please select a protocol (https:// or http://). HTTP is supported if the network environment has access to the public network, and HTTPS is used by defaulthttpProfile.setConnTimeout(30); // Specify the request connection timeout value in seconds. The default value is 60shttpProfile.setWriteTimeout(30); // Set the write timeout period in seconds. The default value is 0shttpProfile.setReadTimeout(30); // Set the read timeout period in seconds. The default value is 0shttpProfile.setEndpoint("cvm.ap-shanghai.tencentcloudapi.com"); // Specify the endpoint. If you do not specify the endpoint, nearby access is enabled by default// Instantiate a client option (optional; skip if no special requirements are present)ClientProfile clientProfile = new ClientProfile();clientProfile.setSignMethod("HmacSHA256"); // Specify the signature algorithm (HmacSHA256 by default)// Starting from v3.1.80, the SDK supports printing logs.clientProfile.setHttpProfile(httpProfile);clientProfile.setDebug(true);// Starting from v3.1.16, the common parameter `Language` can be set, which is not passed in by default. You can select `ZH_CN` or `EN_US`clientProfile.setLanguage(Language.EN_US);// Instantiate the client object of the requested product (with CVM as an example). `clientProfile` is optionalCvmClient client = new CvmClient(cred, "ap-shanghai", clientProfile);// Instantiate a CVM instance information query request object. Each API corresponds to a request objectDescribeInstancesRequest req = new DescribeInstancesRequest();// Populate the request parameters. Here, the member variables of the request object are the input parameters of the corresponding API// You can view the definition of the request parameters in the API documentation at the official website or by redirecting to the definition of the request objectFilter respFilter = new Filter(); // Create a `Filter` object to query CVM instances in the `zone` dimensionrespFilter.setName("zone");respFilter.setValues(new String[] { "ap-shanghai-1", "ap-shanghai-2" });req.setFilters(new Filter[] { respFilter }); // `Filters` is a list of `Filter` objects// Initialize the request by calling the `DescribeInstances` method on the client object. Note: the request method name corresponds to the request object// The returned `resp` is an instance of the `DescribeInstancesResponse` class which corresponds to the request objectDescribeInstancesResponse resp = client.DescribeInstances(req);// A string return packet in JSON format is outputSystem.out.println(DescribeInstancesResponse.toJsonString(resp));// You can also take a single value// You can view the definition of the return field in the API documentation at the official website or by redirecting to the definition of the response objectSystem.out.println(resp.getTotalCount());} catch (TencentCloudSDKException e) {System.out.println(e.toString());}}}
HttpProfile httpProfile = new HttpProfile();httpProfile.setProxyHost("real proxy IP");httpProfile.setProxyPort(real proxy port);
Language to meet the globalization requirements of certain products. As before, Language is not passed in by default subject to the decisions of each service API, which is usually Chinese but can also be English by default in some products. Currently, valid values are Chinese and English, which can be set in the following way:import com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.Language;...ClientProfile clientProfile = new ClientProfile();clientProfile.setLanguage(Language.ZH_CN);
setProtocol() method in HttpProfile:HttpProfile httpProfile = new HttpProfile();httpProfile.setProtocol("http://"); // HTTP protocolhttpProfile.setProtocol("https://"); // HTTPS protocol
ClientProfile object, set the debug mode to true, and the SDK exception information and traffic information will be printed.ClientProfile clientProfile = new ClientProfile();clientProfile.setDebug(true);
commons.logging class to print logs as shown below:SecretId with your real key information.September 10, 2020 5:14:30 PM com.tencentcloudapi.cvm.v20170312.CvmClient infoInformation: send request, request url: https://cvm.ap-shanghai.tencentcloudapi.com/?Nonce=367595572&Action=DescribeInstances&Filters.0.Values.1=ap-shanghai-2&Version=2017-03-12&Filters.0.Values.0=ap-shanghai-1&SecretId=******************&Filters.0.Name=zone&RequestClient=SDK_JAVA_3.1.129&Region=ap-shanghai&SignatureMethod=HmacSHA256&Timestamp=1599729270&Signature=DcGRPdquMZZRPj1NFXP5bsOGnRlaT2KXy7aegNhZa00%3D. request headers information:September 10, 2020 5:14:32 PM com.tencentcloudapi.cvm.v20170312.CvmClient infoInformation: recieve response, response url: https://cvm.ap-shanghai.tencentcloudapi.com/?Nonce=367595572&Action=DescribeInstances&Filters.0.Values.1=ap-shanghai-2&Version=2017-03-12&Filters.0.Values.0=ap-shanghai-1&SecretId=******************&Filters.0.Name=zone&RequestClient=SDK_JAVA_3.1.129&Region=ap-shanghai&SignatureMethod=HmacSHA256&Timestamp=1599729270&Signature=DcGRPdquMZZRPj1NFXP5bsOGnRlaT2KXy7aegNhZa00%3D, response headers: Server: nginx;Date: Thu, 10 Sep 2020 09:14:32 GMT;Content-Type: application/json;Content-Length: 103;Connection: keep-alive;OkHttp-Selected-Protocol: http/1.1;OkHttp-Sent-Millis: 1599729271230;OkHttp-Received-Millis: 1599729272020;,response body information: com.squareup.okhttp.internal.http.RealResponseBody@8646db9
log4j, in the following way:pom file and set the log4j version.<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
log4j and create a log class.System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");Log logger = LogFactory.getLog("TestLog");logger.info("hello world");
pom.xml:<dependency><groupId>com.qcloud</groupId><artifactId>qcloud-java-sdk</artifactId><version>2.0.6</version></dependency>
integer fields have been modified to long type, the project needs to be recompiled.Exception in thread "main" java.lang.NoSuchMethodError: okio.BufferedSource.rangeEquals(JLokio/ByteString;)Z. The reason is that OkHttp 3 depends on Okio 1.12.0, while OkHttp 2.5.0 depends on Okio 1.6.0. When Maven parses dependencies, it gives top priority to the shortest path in sequence, so if the SDK is declared in the pom.xml dependency first, Okio 1.6.0 will be used, and an error will be reported.<dependency><groupId>com.squareup.okio</groupId><artifactId>okio</artifactId><version>1.12.0</version></dependency>
<dependency><groupId>com.qcloud</groupId><artifactId>cmq-http-client</artifactId><version>1.0.7</version></dependency><dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.59</version></dependency>
-Djavax.net.debug=ssl to enable detailed logs for troubleshooting.javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure occurred while using IBM JDK 1.8. The error was resolved after Oracle JDK was used.Common Client mode for requests. You only need to install the Common package to initiate calls to any Tencent Cloud product.TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY are read to get secretId and secretKey. The relevant code is as follows:Credential cred = new EnvironmentVariableCredentialsProvider().getCredentials();
c:\\Users\\NAME\\.tencentcloud\\credentials~/.tencentcloud/credentials or /etc/tencentcloud/credentials[default]secret_id = xxxxxsecret_key = xxxxx
Credential cred = new ProfileCredentialsProvider().getCredentials();
roleArn, and the SDK will automatically refresh the temporary credentials. The relevant code is as follows:Credential cred = new STSCredential("secretId", "secretKey", "roleArn", "roleSessionName");
Credential cred = new CvmRoleCredential();
Credential cred = new DefaultCredentialsProvider().getCredentials();
Feedback