Java SDK
1. 准备工作
1) 获取API密钥信息
在调用API之前,您需要获取用于生成X-Signature
签名的关键信息。您需要提供AccessKeyId
和AccessKeySecret
,这些信息可以从控制台账户中获取。有关如何获取这些信息的详细步骤,请参考:如何获取AccessKeyId和AccessKeySecret。
2) 申请短信模板
2. SDK
1) 使用maven进行安装(推荐)并配置pom.xml:
<dependency>
<groupId>com.uspeedo</groupId>
<artifactId>uspeedo-sdk-java-asms</artifactId>
<version>1.3.1-release</version>
</dependency>
3. 示例
请查看github链接了解详情:uspeedo-sdk-java
package com.uspeedo.example;
import com.uspeedo.common.config.Config;
import com.uspeedo.common.credential.Credential;
import com.uspeedo.common.exception.USpeedoException;
import com.uspeedo.asms.models.*;
import com.uspeedo.asms.client.AsmsClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collections;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void invokeSendMessage() {
AsmsClient asmsClient = getClient();
SendBatchUSMSMessageReq req = new SendBatchUSMSMessageReq();
SendBatchUSMSMessageReq.SendInfo info = new SendBatchUSMSMessageReq.SendInfo();
SendBatchUSMSMessageReq.TargetPhone targetPhone = new SendBatchUSMSMessageReq.TargetPhone();
info.setSenderId("uspeedo");
info.setTemplateId("UTAXXXXXXXXXXX");
targetPhone.setPhone("(11)11111111");
String[] templateParams = {"xxxx"};
targetPhone.setTemplateParams(Arrays.asList(templateParams));
info.setTarget(Collections.singletonList(targetPhone));
req.setTaskContent(Collections.singletonList(info));
try {
SendBatchUSMSMessageResp resp = asmsClient.sendBatchUSMSMessage(req);
System.out.println(resp);
} catch (USpeedoException e) {
System.out.println(e.getMessage());
}
}
public static void invokeTemplateAPi() {
AsmsClient asmsClient = getClient();
// 创建模板
CreateUSMSTemplateReq createUSMSTemplateReq = new CreateUSMSTemplateReq();
createUSMSTemplateReq.setTemplate("Your verification code is1 {1}");
createUSMSTemplateReq.setPurpose(1);
createUSMSTemplateReq.setRemark("this is a test template");
createUSMSTemplateReq.setTemplateName("test template");
try {
CreateUSMSTemplateResp createUSMSTemplateResp = asmsClient.createUSMSTemplate(createUSMSTemplateReq);
System.out.println(createUSMSTemplateResp);
} catch (USpeedoException e) {
System.out.println(e.getMessage());
}
// 查询模板
QueryUSMSTemplateReq queryTemplateReq = new QueryUSMSTemplateReq();
String[] templateIds = {"UTAxxxxxxxxx"};
queryTemplateReq.setTemplateId(Arrays.asList(templateIds));
try {
QueryUSMSTemplateResp queryTemplateResp = asmsClient.queryUSMSTemplate(queryTemplateReq);
System.out.println(queryTemplateResp.getData());
} catch (USpeedoException e) {
System.out.println(e.getMessage());
}
// 更新模板
UpdateUSMSTemplateReq updateTemplateReq = new UpdateUSMSTemplateReq();
updateTemplateReq.setTemplateId("UTAxxxxxxxxx");
updateTemplateReq.setTemplate("Your verification code is {1},thanks");
try {
UpdateUSMSTemplateResp updateTemplateResp = asmsClient.updateUSMSTemplate(updateTemplateReq);
System.out.println(updateTemplateResp);
} catch (USpeedoException e) {
System.out.println(e.getMessage());
}
// 删除模板
DeleteUSMSTemplateReq deleteTemplateReq = new DeleteUSMSTemplateReq();
deleteTemplateReq.setTemplateIds(Arrays.asList(templateIds));
try {
DeleteUSMSTemplateResp deleteTemplateResp = asmsClient.deleteUSMSTemplate(deleteTemplateReq);
System.out.println(deleteTemplateResp);
} catch (USpeedoException e) {
System.out.println(e.getMessage());
}
}
public static AsmsClient getClient() {
Config config = new Config();
config.setLogger(logger);
String publicKey = System.getenv("USpeedo_PUBLIC_KEY");
String privateKey = System.getenv("USpeedo_PRIVATE_KEY");
Credential credential = new Credential(publicKey, privateKey);
return new AsmsClient(config, credential);
}
public static void main(String[] args) {
// invokeTemplateAPi();
invokeSendMessage();
}
}