接入腾讯云短信服务(史上最详细+该短信服务如何申请成功+发送短信验证码API讲解+相关错误分析)
2021/8/17/23:01{2021/8/17/23:01}2021/8/17/23:01
文章目錄
- 前言
- 一、如何成功申請到騰訊云短信服務
- 1、簽名申請
- 2、正文模板申請
- 二、發送短信API開發
- 1、騰訊云在線測試
- 2、使用springboot進行測試
- 三、常見的錯誤分析
前言
或許有的人會問我為什么不使用阿里云短信服務,那是因為如今的阿里云短信服務對于大部分人來說很難申請成功,它需要備案或者項目已經上線。而騰訊云短信相對來說比較寬松一些,可以利用個人公眾號進行申請成功
提示:以下是本篇文章正文內容
一、如何成功申請到騰訊云短信服務
首先開通短信服務,應該會免費贈送100條短信
1、簽名申請
我已經測試申請過一次{我已經測試申請過一次}我已經測試申請過一次
點擊創建簽名
微信公眾號平臺鏈接:https://www.baidu.com/link?url=3OqiiGcBpe8Gp5YpnN4wF7CiaFkigOjrPtN3xjuIWLF1EDlRASXa3EDgeaQNn8k6&wd=&eqid=d95710f900018b6500000003611bd16a
2、正文模板申請
點擊創建正文模板
等待審核即可
二、發送短信API開發
1、騰訊云在線測試
先進行在線測試,后面再使用代碼測試
測試地址:https://console.cloud.tencent.com/api/explorer?Product=sms&Version=2021-01-11&Action=SendSms&SignVersion=
1、SmsSdkAppId:短信 SdkAppId,在 短信控制臺 添加應用后生成的實際 SdkAppId
2、TemplateId:模板 ID,必須填寫已審核通過的模板 ID。模板 ID 可登錄 短信控制臺 查看,若向境外手機號發送短信,僅支持使用國際/港澳臺短信模板。
3、SignName:短信簽名內容,使用 UTF-8 編碼,必須填寫已審核通過的簽名,例如:騰訊云,簽名信息可登錄 短信控制臺 查看。 國內短信為必填參數。
點擊發送請求
成功!
2、使用springboot進行測試
1.文檔參考準備工作
可以在云端直接進行生成代碼
也可以參考API文檔
API文檔鏈接地址:https://cloud.tencent.com/document/product/382/43194
2.導入依賴
3.application.yml{application.yml}application.yml文件編寫
4.創建工具類
//實現了InitializingBean接口,當spring進行初始化bean時,會執行afterPropertiesSet方法 @Component public class MsmConstantUtils implements InitializingBean {//我已經再@Value("${tencent.msm.id}")private String secretID ;@Value("${tencent.msm.secret}")private String secretKey ;@Value("${tencent.msm.endPoint}")private String endPoint;@Value("${tencent.msm.appId}")private String appId;@Value("${tencent.msm.signName}")private String signName;@Value("${tencent.msm.templateId}")private String templateId;//六個相關的參數public static String SECRET_ID;public static String SECRET_KEY;public static String END_POINT;public static String APP_ID;public static String SIGN_NAME;public static String TEMPLATE_ID;@Overridepublic void afterPropertiesSet() throws Exception {SECRET_ID = secretID;SECRET_KEY = secretKey;END_POINT = endPoint;APP_ID = appId;SIGN_NAME = signName;TEMPLATE_ID = templateId;} }5.RandomUtil編寫
public class RandomUtil {private static final Random random = new Random(); //我定義的驗證碼位數是6位private static final DecimalFormat sixdf = new DecimalFormat("000000");public static String getSixBitRandom() {return sixdf.format(random.nextInt(1000000));} }6、接口編寫
注意我找的這個項目已經集成了Swagger配置,后面我需要進行測試
@RestController @RequestMapping("/msm") @CrossOrigin @Api("發送短信服務") public class MsmController {@Autowiredprivate MsmService msmService;@ApiOperation("發送短信")@GetMapping("/send/{phone}")public ResponseEntity send(@PathVariable String phone) {boolean send = msmService.send(phone);if (send) {return ResponseEntity.ok();}return ResponseEntity.error();} } import com.tencentcloudapi.common.Credential; 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; import com.zhang.service.MsmService; import com.zhang.utils.MsmConstantUtils; import com.zhang.utils.RandomUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;/*** @author:zsh* @date:2021/8/18* @email:181@qq.com*/ @Service @Slf4j public class MsmServiceImpl implements MsmService {@Overridepublic boolean send(String phone) {try {//這里是實例化一個Credential,也就是認證對象,參數是密鑰對;你要使用肯定要進行認證Credential credential = new Credential(MsmConstantUtils.SECRET_ID, MsmConstantUtils.SECRET_KEY);//HttpProfile這是http的配置文件操作,比如設置請求類型(post,get)或者設置超時時間了、還有指定域名了//最簡單的就是實例化該對象即可,它的構造方法已經幫我們設置了一些默認的值HttpProfile httpProfile = new HttpProfile();//這個setEndpoint可以省略的httpProfile.setEndpoint(MsmConstantUtils.END_POINT);//實例化一個客戶端配置對象,這個配置可以進行簽名(使用私鑰進行加密的過程),對方可以利用公鑰進行解密ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);//實例化要請求產品(以sms為例)的client對象SmsClient smsClient = new SmsClient(credential, "ap-beijing", clientProfile);//實例化request封裝請求信息SendSmsRequest request = new SendSmsRequest();String[] phoneNumber = {phone};request.setPhoneNumberSet(phoneNumber); //設置手機號request.setSmsSdkAppid(MsmConstantUtils.APP_ID);request.setSign(MsmConstantUtils.SIGN_NAME);request.setTemplateID(MsmConstantUtils.TEMPLATE_ID);//生成隨機驗證碼,我的模板內容的參數只有一個String verificationCode = RandomUtil.getSixBitRandom();String[] templateParamSet = {verificationCode};request.setTemplateParamSet(templateParamSet);//發送短信SendSmsResponse response = smsClient.SendSms(request);log.info(SendSmsResponse.toJsonString(response));return true;} catch (Exception e) {return false;}} }7.測試
訪問http://localhost:8005/swagger-ui.html
使用swagger進行測試即可
成功!
三、常見的錯誤分析
| 騰訊云短信出現there are both domestic mobile phone numbers and international mobile phone numbers in the… | https://blog.csdn.net/Kevinnsm/article/details/119767611?spm=1001.2014.3001.5501 |
結束了! ??
2021/8/18/0:54
總結
以上是生活随笔為你收集整理的接入腾讯云短信服务(史上最详细+该短信服务如何申请成功+发送短信验证码API讲解+相关错误分析)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯云短信出现there are bot
- 下一篇: 网站如何接入微信支付功能?微信支付详细教