SecretID
and SecretKey
on the API Key Management page in the CAM console.SecretID
is used to identify the API caller.SecretKey
is used to encrypt the string to sign that can be verified on the server. You should keep it private and avoid disclosure.sms.tencentcloudapi.com
.import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;// Import the optional configuration classesimport com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;// Import the client of the corresponding SMS moduleimport com.tencentcloudapi.sms.v20210111.SmsClient;// Import the request response class corresponding to the request APIimport com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;/*** Tencent Cloud Sms Sendsms**/public class SendSms{public static void main(String[] args){try {// Instantiate an authentication object, input parameters require Tencent Cloud account SecretId and SecretKey. // To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.// SecretId, SecretKey inquiry: https://console.tencentcloud.com/cam/capi // Credential cred = new Credential("SecretId", "SecretKey"); Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));// Instantiate an HTTP option, optional, can be skipped if there are no special requirements HttpProfile httpProfile = new HttpProfile(); // Starting from version 3.0.96, an HTTP proxy can be set separately (ignore it if not needed) // httpProfile.setProxyHost("Real proxy IP"); // httpProfile.setProxyPort(Real proxy port); httpProfile.setReqMethod("GET"); // GET request (the default value is POST request) httpProfile.setConnTimeout(10); // Request connection timeout time, in seconds (the default value is 60 seconds) httpProfile.setWriteTimeout(10); // Sets write timeout time, in seconds (the default value is 0 seconds) httpProfile.setReadTimeout(10); // Sets read timeout time, in seconds (the default value is 0 seconds)/* Specifies the access region domain name. The default nearby region domain name is sms.tencentcloudapi.com. Specifying a region domain name for access is also supported. For example, the domain name for the Singapore region is sms.ap-singapore.tencentcloudapi.com */httpProfile.setEndpoint("sms.tencentcloudapi.com");/* Optional steps:* Instantiate a client configuration object. You can specify the timeout period and other configuration items */ClientProfile clientProfile = new ClientProfile();/* The SDK uses `TC3-HMAC-SHA256` to sign by default* Do not modify this field unless absolutely necessary */clientProfile.setSignMethod("HmacSHA256");clientProfile.setHttpProfile(httpProfile);/* Instantiate the client object of the requested product (with SMS as an example)* The second parameter is the information on the region you select in Tencent Cloud International. If you select Singapore, you should enter the string `ap-singapore`. Click https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list to view the region list. */SmsClient client = new SmsClient(cred, "ap-singapore",clientProfile);/* Instantiate a request object. You can further set the request parameters according to the API called and actual conditions* You can directly check the SDK source code to determine which attributes of the API can be set* An attribute may be of a basic type or import another data structure* We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure */SendSmsRequest req = new SendSmsRequest();/* 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 object* Settings of a basic parameter:* Help link:* SMS console: https://console.tencentcloud.com/smsv2* sms helper: https://www.tencentcloud.com/document/product/382/3773 *//* SMS application ID, which is the `SdkAppId` generated after an application is added in the [SMS console], such as 2400006666 */String sdkAppId = "2400006666";req.setSmsSdkAppId(sdkAppId);/* SMS signature content, which should be encoded in UTF-8. You must enter an approved signature, which can be viewed in the [SMS console] */String signName = "Signature content";req.setSignName(signName);/* `SenderId` for Global SMS, which is not activated by default. If you need to activate it, please contact [SMS Helper] for assistance. This parameter should be left empty for Mainland China SMS */String senderid = "";req.setSenderId(senderid);/* User session content, which can carry context information such as user-side ID and will be returned as-is by the server */String sessionContext = "xxx";req.setSessionContext(sessionContext);/* SMS code number extension, which is not activated by default. If you need to activate it, please contact [SMS Helper] */String extendCode = "";req.setExtendCode(extendCode);/* Template ID. You must enter the ID of an approved template, which can be viewed in the [SMS console] */String templateId = "400000";req.setTemplateId(templateId);/* Target mobile number in the E.164 standard (+[country/region code][mobile number])* Example: +60198890000, which has a + sign followed by 60 (country/region code) and then by 198890000 (mobile number). Up to 200 mobile numbers are supported */String[] phoneNumberSet = {"+60198890000", "+60198890001", "+60198890002"};req.setPhoneNumberSet(phoneNumberSet);/* Template parameters. If there are no template parameters, leave it empty */String[] templateParamSet = {"5678"};req.setTemplateParamSet(templateParamSet);/* Initialize the request by calling the `SendSms` method on the client object. Note: the request method name corresponds to the request object* The returned `res` is an instance of the `SendSmsResponse` class which corresponds to the request object */SendSmsResponse res = client.SendSms(req);// A string return packet in JSON format is outputSystem.out.println(SendSmsResponse.toJsonString(res));// 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(res.getRequestId());} catch (TencentCloudSDKException e) {e.printStackTrace();}}}
import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;// Import the optional configuration classesimport com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;// Import the client of the corresponding SMS moduleimport com.tencentcloudapi.sms.v20210111.SmsClient;// Import the request response class corresponding to the request APIimport com.tencentcloudapi.sms.v20210111.models.PullSmsReplyStatusRequest;import com.tencentcloudapi.sms.v20210111.models.PullSmsReplyStatusResponse;/*** Tencent Cloud Sms PullSmsSendStatus**/public class PullSmsSendStatus {public static void main(String[] args) {try {// Instantiate an authentication object, input parameters require Tencent Cloud account SecretId and SecretKey. // To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended. // SecretId, SecretKey inquiry: https://console.tencentcloud.com/cam/capi // Credential cred = new Credential("SecretId", "SecretKey"); Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));// Instantiate an HTTP option, optional, can be skipped if there are no special requirements HttpProfile httpProfile = new HttpProfile(); // Starting from version 3.0.96, an HTTP proxy can be set separately (ignore it if not needed) // httpProfile.setProxyHost("Real proxy IP"); // httpProfile.setProxyPort(Real proxy port); httpProfile.setReqMethod("GET"); // GET request (the default value is POST request) httpProfile.setConnTimeout(30); // Request connection timeout time, in seconds (the default value is 60 seconds) httpProfile.setWriteTimeout(30); // Sets write timeout time, in seconds (the default value is 0 seconds) httpProfile.setReadTimeout(30); // Sets read timeout time, in seconds (the default value is 0 seconds)/* Specifies the access region domain name. The default nearby region domain name is sms.tencentcloudapi.com. Specifying a region domain name for access is also supported. For example, the domain name for the Singapore region is sms.ap-singapore.tencentcloudapi.com */httpProfile.setEndpoint("sms.tencentcloudapi.com");/* Optional steps:* Instantiate a client configuration object. You can specify the timeout period and other configuration items */ClientProfile clientProfile = new ClientProfile();/* The SDK uses `TC3-HMAC-SHA256` to sign by default* Do not modify this field unless absolutely necessary */clientProfile.setSignMethod("HmacSHA256");clientProfile.setHttpProfile(httpProfile);/* Instantiate the client object of the requested product (with SMS as an example)* The second parameter is the information on the region you select in Tencent Cloud International. If you select Singapore, you should enter the string `ap-singapore`. Click https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list to view the region list. */SmsClient client = new SmsClient(cred, "ap-singapore", clientProfile);/* Instantiate a request object. You can further set the request parameters according to the API called and actual conditions* You can directly check the SDK source code to determine which attributes of the API can be set* An attribute may be of a basic type or import another data structure* We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure */PullSmsSendStatusRequest req = new PullSmsSendStatusRequest();/* 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 object* Settings of a basic parameter:* Help link:* SMS console: https://console.tencentcloud.com/smsv2* sms helper: https://www.tencentcloud.com/document/product/382/3773 *//* SMS application ID, which is the `SdkAppId` generated after an application is added in the [SMS console], such as 2400006666 */String sdkAppId = "2400006666";req.setSmsSdkAppId(sdkAppId);// Set the maximum number of pulled entries. Maximum value: 100Long limit = 5L;req.setLimit(limit);/* Initialize the request by calling the `PullSmsSendStatus` method on the client object. Note: the request method name corresponds to the request object* The returned `res` is an instance of the `PullSmsSendStatusResponse` class which corresponds to the request object */PullSmsSendStatusResponse res = client.PullSmsSendStatus(req);// A string return packet in JSON format is outputSystem.out.println(PullSmsSendStatusResponse.toJsonString(res));} catch (TencentCloudSDKException e) {e.printStackTrace();}}}
import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;// Import the optional configuration classesimport com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;// Import the client of the corresponding SMS moduleimport com.tencentcloudapi.sms.v20210111.SmsClient;// Import the request response class corresponding to the request APIimport com.tencentcloudapi.sms.v20210111.models.SendStatusStatisticsRequest;import com.tencentcloudapi.sms.v20210111.models.SendStatusStatisticsResponse;/*** Tencent Cloud Sms SendStatusStatistics**/public class SendStatusStatistics {public static void main(String[] args) {try {// Instantiate an authentication object, input parameters require Tencent Cloud account SecretId and SecretKey. // To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.// SecretId, SecretKey inquiry: https://console.tencentcloud.com/cam/capi // Credential cred = new Credential("SecretId", "SecretKey"); Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));// Instantiate an HTTP option, optional, can be skipped if there are no special requirements HttpProfile httpProfile = new HttpProfile(); // Starting from version 3.0.96, an HTTP proxy can be set separately (ignore it if not needed) // httpProfile.setProxyHost("Real proxy IP"); // httpProfile.setProxyPort(Real proxy port); httpProfile.setReqMethod("GET"); // GET request (the default value is POST request) httpProfile.setConnTimeout(30); // Request connection timeout time, in seconds (the default value is 60 seconds) httpProfile.setWriteTimeout(30); // Sets write timeout time, in seconds (the default value is 0 seconds) httpProfile.setReadTimeout(30); // Sets read timeout time, in seconds (the default value is 0 seconds)/* Specifies the access region domain name. The default nearby region domain name is sms.tencentcloudapi.com. Specifying a region domain name for access is also supported. For example, the domain name for the Singapore region is sms.ap-singapore.tencentcloudapi.com */httpProfile.setEndpoint("sms.tencentcloudapi.com");/* Optional steps:* Instantiate a client configuration object. You can specify the timeout period and other configuration items */ClientProfile clientProfile = new ClientProfile();/* The SDK uses `TC3-HMAC-SHA256` to sign by default* Do not modify this field unless absolutely necessary */clientProfile.setSignMethod("HmacSHA256");clientProfile.setHttpProfile(httpProfile);/* Instantiate the client object of the requested product (with SMS as an example)* The second parameter is the information on the region you select in Tencent Cloud International. If you select Singapore, you should enter the string `ap-singapore`. Click https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list to view the region list. */SmsClient client = new SmsClient(cred, "ap-singapore",clientProfile);/* Instantiate a request object. You can further set the request parameters according to the API called and actual conditions* You can directly check the SDK source code to determine which attributes of the API can be set* An attribute may be of a basic type or import another data structure* We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure */SendStatusStatisticsRequest req = new SendStatusStatisticsRequest();/* 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 object* Settings of a basic parameter:* Help link:* SMS console: https://console.tencentcloud.com/smsv2* sms helper: https://www.tencentcloud.com/document/product/382/3773 *//* SMS application ID, which is the `SdkAppId` generated after an application is added in the [SMS console], such as 2400006666 */String sdkAppId = "2400006666";req.setSmsSdkAppId(sdkAppId);// Set the maximum number of pulled entries. Maximum value: 100Long limit = 5L;req.setLimit(limit);/* Offset. Note: this parameter is currently fixed at 0 */Long offset = 0L;req.setOffset(offset);/* Start time of pull in the format of `yyyymmddhh` accurate to the hour */String beginTime = "2019071100";req.setBeginTime(beginTime);/* End time of pull in the format of `yyyymmddhh` accurate to the hour* Note: `EndTime` must be after `beginTime` */String endTime = "2019071123";req.setEndTime(endTime);/* Initialize the request by calling the `SendStatusStatistics` method on the client object. Note: the request method name corresponds to the request object* The returned `res` is an instance of the `SendStatusStatisticsResponse` class which corresponds to the request object */SendStatusStatisticsResponse res = client.SendStatusStatistics(req);// A string return packet in JSON format is outputSystem.out.println(SendStatusStatisticsResponse.toJsonString(res));} catch (TencentCloudSDKException e) {e.printStackTrace();}}}
import com.tencentcloudapi.common.Credential;import com.tencentcloudapi.common.exception.TencentCloudSDKException;// Import the optional configuration classesimport com.tencentcloudapi.common.profile.ClientProfile;import com.tencentcloudapi.common.profile.HttpProfile;// Import the client of the SMS moduleimport com.tencentcloudapi.sms.v20210111.SmsClient;// Import the request response class corresponding to the request APIimport com.tencentcloudapi.sms.v20210111.models.AddSmsTemplateRequest;import com.tencentcloudapi.sms.v20210111.models.AddSmsTemplateResponse;/*** Tencent Cloud Sms AddSmsTemplate**/public class AddSmsTemplate{public static void main( String[] args ){try {// Instantiate an authentication object, input parameters require Tencent Cloud account SecretId and SecretKey. // To protect key security, it is suggested to set keys in environment variables or configuration files. // Hardcoding keys into the code might lead to exposure through leaked code, posing a security vulnerability, and is not recommended.// SecretId, SecretKey inquiry: https://console.tencentcloud.com/cam/capi // Credential cred = new Credential("SecretId", "SecretKey"); Credential cred = new Credential(System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));// Instantiate an HTTP option, optional, can be skipped if there are no special requirements HttpProfile httpProfile = new HttpProfile(); // Starting from version 3.0.96, an HTTP proxy can be set separately (ignore it if not needed) // httpProfile.setProxyHost("Real proxy IP"); // httpProfile.setProxyPort(Real proxy port); httpProfile.setReqMethod("GET"); // GET request (the default value is POST request) httpProfile.setConnTimeout(30); // Request connection timeout time, in seconds (the default value is 60 seconds) httpProfile.setWriteTimeout(30); // Sets write timeout time, in seconds (the default value is 0 seconds) httpProfile.setReadTimeout(30); // Sets read timeout time, in seconds (the default value is 0 seconds)/* Specifies the access region domain name. The default nearby region domain name is sms.tencentcloudapi.com. Specifying a region domain name for access is also supported. For example, the domain name for the Singapore region is sms.ap-singapore.tencentcloudapi.com */httpProfile.setEndpoint("sms.tencentcloudapi.com");/* Optional steps:* Instantiate a client configuration object. You can specify the timeout period and other configuration items */ClientProfile clientProfile = new ClientProfile();/* The SDK uses `TC3-HMAC-SHA256` to sign by default* Do not modify this field unless absolutely necessary */clientProfile.setSignMethod("HmacSHA256");clientProfile.setHttpProfile(httpProfile);/* Instantiate an SMS client object* The second parameter is the information on the region you select in Tencent Cloud International. If you select Singapore, you should enter the string `ap-singapore`. Click https://www.tencentcloud.com/document/api/382/40466?lang=en#region-list to view the region list. */SmsClient client = new SmsClient(cred, "ap-singapore", clientProfile);/* Instantiate a request object. You can further set the request parameters according to the API called and actual conditions* You can directly check the SDK source code to determine which attributes of the API can be set* An attribute may be of a basic type or import another data structure* We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure */AddSmsTemplateRequest req = new AddSmsTemplateRequest();/* 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 object* Settings of a basic parameter:* Help link:* SMS console: https://console.tencentcloud.com/smsv2* sms helper:https://www.tencentcloud.com/document/product/382/3773 *//* Template name */String templatename = "Tencent Cloud";req.setTemplateName(templatename);/* Template content */String templatecontent = "Your login verification code is {1}. Please enter it within {2} minutes. If the login was not initiated by you, please ignore this message.";req.setTemplateContent(templatecontent);/* SMS type. 1: Marketing SMS, 2: Notification SMS, 3: OTP SMS */long smstype = 3;req.setSmsType(smstype);/* Whether it is Global SMS. 0: Mainland China SMS; 1: Global SMS */long international = 0;req.setInternational(international);/* Template remarks, such as reason for application and use case */String remark = "xxx";req.setRemark(remark);/* Initialize the request by calling the `AddSmsTemplate` method on the client object. Note: the request method name corresponds to the request object* The returned `res` is an instance of the `AddSmsTemplateResponse` class which corresponds to the request object */AddSmsTemplateResponse res = client.AddSmsTemplate(req);// A string return packet in JSON format is outputSystem.out.println(AddSmsTemplateResponse.toJsonString(res));// You can 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(res.getRequestId());} catch (TencentCloudSDKException e) {e.printStackTrace();}}}
java.lang.NoSuchMethodError: xxx.setSkipSign
. This is due to a version mismatch between the tencentcloud-sdk-java-common
package and the tencentcloud-sdk-java-sms
package.common
package in the pom, or by indirectly referencing an incompatible common
version due to the reference to other third-party SDKs.common
package in pom.xml. For example, if the version of tencentcloud-sdk-java-sms
is 3.1.1000
, you can specify the common
package version like this:<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java-common</artifactId><version>3.1.1000</version></dependency>
java.lang.NoSuchMethodError: kotlin.collections.ArraysKt.copyInto
. This is due to the Kotlin runtime environment being of a lower version, and upgrading the Kotlin version might resolve the issue.[TencentCloudSDKException]message:java.net.ConnectException-Connection timed out: connect requestId:
, you need to check whether the local server has a proxy configured but the proxy is not added in the code. For more information, please see below:HttpProfile httpProfile = new HttpProfile();httpProfile.setProxyHost("actual proxy IP");httpProfile.setProxyPort(real proxy port);
integer
fields have been modified to long
type, the project needs to be recompiled.-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.
この記事はお役に立ちましたか?