我正在尝试一个小型演示,用于将SMS发送到印度的号码。我使用了以下代码段,但无法正常工作。
public void send(String to, String textBody) {
AWSCredentials awsCredentials;
awsCredentials = new EnvironmentVariableCredentialsProvider().getCredentials();
AmazonSNSClient snsClient = new AmazonSNSClient(awsCredentials);
PublishResult result = snsClient
.publish(new PublishRequest().withMessage(textBody)
.withPhoneNumber("+9195355*****"));
System.out.println(result);
}
我不知道丢失了什么?
由于您已确认已使用SNS控制台发送SMS,因此消息已成功发送,这似乎表明所提供的代码段可能存在问题!
我已经稍微修改了您的代码段,并确认以下代码段可以正常使用:
public static void main(String[] args) {
ProfileCredentialsProvider creds = new ProfileCredentialsProvider("syumaK");
AmazonSNS snsClient = AmazonSNSClientBuilder.standard().withCredentials(creds).withRegion("us-east-1").build();
String textBody = "My test SMS message from SNS";
String to = "+0123456789";
send(snsClient, to, textBody);
}
public static void send(AmazonSNS snsClient, String to, String textBody) {
try {
PublishResult result = snsClient
.publish(new PublishRequest()
.withMessage(textBody)
.withPhoneNumber(to));
System.out.println(result);
LOGGER.info("Message send with id {}."+ result.getMessageId());
} catch (AmazonSNSException e) {
LOGGER.info("Got an exception. " + e.getMessage())
}
}
故障排除步骤:
-如果使用ProfileCredentialsProvider的工作原理与上述相同,请考虑切换回EnvironmentVariableCredentialsProvider或使用BasicAWSCredentials提供程序,并检查是否遵守以下规定:成功/失败?
我已经使用以下环境规范测试了上述代码段:
OS : Ubuntu 16.04
aws region: us-east-1
aws-java-sdk: "1.11.723"