腾讯云短信接口调用案例超简单

一开始百度查了一下,层次不齐,有的弄得很复杂。
昨天刚刚调好了分享一下给大家,少采坑,下面任何一个参数内容都是必填项。

腾讯云短信后台找到这个六个参数,依次对应。
注意参数
必须按照这个参数格式,

TemplateParamSet 的3是我写死了3,他是正文模板时间参数,可以不做修改。
这里只要改sign 参数需要替换一下,签名内容在腾讯云短信后台签名模块可以查到。

  String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"]," +
                    "\"TemplateID\":\""+TEMPLATE_ID+"\",\"Sign\":\"sign短信签名内容不是id\"," +
                    "\"TemplateParamSet\":[\"" + code + "\",\"3\"]," +
                    "\"SmsSdkAppid\":\""+SMS_SDK_APP_ID+"\"}";
package com.df.detection.base.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

public class SmsUtils {

    public static void sendRegCode(String phone, String code)  {
        //腾讯云API密钥ID
        String SECRET_ID = "SECRET_ID ";

        //腾讯云API密钥ID
        String SECRET_KEY = "SECRET_KEY ";

        //腾讯云API
        String URL = "sms.tencentcloudapi.com";

        //腾讯云短信模板ID
        String TEMPLATE_ID = "短信模板ID";

        //腾讯云应用ID SDK_APP_ID
        String SMS_SDK_APP_ID = "14XXXXXX";
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(URL);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "", clientProfile);
            System.out.println(code);
            System.out.println(phone);
            String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"]," +
                    "\"TemplateID\":\""+TEMPLATE_ID+"\",\"Sign\":\"sign短信签名内容不是id\"," +
                    "\"TemplateParamSet\":[\"" + code + "\",\"3\"]," +
                    "\"SmsSdkAppid\":\""+SMS_SDK_APP_ID+"\"}";
            SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
            SendSmsResponse resp = client.SendSms(req);
            System.out.println(SendSmsRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }

}

Maven短信依赖

   <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.69</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
        </dependency>

你可能感兴趣的:(腾讯云短信接口调用案例超简单)