(1)注册用户,并且开通短信套餐
(2)
点击快速学习,然后绑定测试的手机号码。
选用专用测试签名(自定义的话阿里可能会验证什么什么的比较麻烦)
然后在选取调用API
(3)
鼠标滑动右上角,然后点击AccessKey,进入页面创建AccessKey,之后将key和secret保存下来。
(4)
这是进入的调用短信API的页面,然后开始复制粘贴代码
@DubboService @Service public class MessageServiceImpl implements MessageService { @Value("${message.accessKeyId}") private String accessKeyId; @Value("${message.accessKeySecret}") private String accessKeySecret; @SneakyThrows @Override public BaseResult sendMessage(String phoneNumber, String code) { //第一个参数:手机号 //第二个参数验证码 StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder() .accessKeyId(accessKeyId) .accessKeySecret(accessKeySecret) .build()); AsyncClient client = AsyncClient.builder() .region("cn-hangzhou") // Region ID .credentialsProvider(provider) .overrideConfiguration( ClientOverrideConfiguration.create() .setEndpointOverride("dysmsapi.aliyuncs.com") ) .build(); // Parameter settings for API request SendSmsRequest sendSmsRequest = SendSmsRequest.builder() .signName("阿里云短信测试") .templateCode("SMS_154950909") .phoneNumbers(phoneNumber) .templateParam("{\"code\":\"" + code + "\"}") .build(); CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest); SendSmsResponse resp = response.get(); client.close(); SendSmsResponseBody respBody = resp.getBody(); if ("OK".equals(respBody.getCode())) { return new BaseResult(200, respBody.getCode(), respBody.getMessage()); } else { return new BaseResult(500, respBody.getCode(), respBody.getMessage()); } } }