文章目錄
- 前言
- 一、如何成功申請到騰訊雲短信服務
- 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.導入依賴
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
3.檔案編寫
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;
@Override
public 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 {
@Autowired
private 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:[email protected]
*/
@Service
@Slf4j
public class MsmServiceImpl implements MsmService {
@Override
public 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進行測試即可
成功!
三、常見的錯誤分析
使用騰訊雲短信服務技術出現FailedOperation.TemplateIncorrectOrUnapproved | javascript:void(0) |
騰訊雲短信出現there are both domestic mobile phone numbers and international mobile phone numbers in the… | javascript:void(0) |