在我的产品中,我必须验证用户的电话号码。为此,我想通过短信发送验证码。但是,我找不到通过golang发送验证码的参考
- 这是我的密码
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
)
func main() {
fmt.Println("creating session")
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1"),
}))
fmt.Println("session created")
svc := sns.New(sess)
fmt.Println("service created")
params := &sns.PublishInput{
Message: aws.String("testing 123"),
PhoneNumber: aws.String(<"myPhoneNumber">),
}
resp, err := svc.Publish(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}
我得到了这样的回应
creating session
session created
service created
{
MessageId: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX"
}
但是消息没有到达我这里。而且,我不知道如何使用MessageId来检查响应结果
我可以得到一些参考资料吗?或者我需要先了解其他功能吗?请帮助我🙏
我刚才搜索了很多词,如"aws短信使用golang","aws验证码短信golang",但它没有为我工作
- 这是我第二次尝试了,用JS!但是还是不行🥹
let { SNSClient } = require("@aws-sdk/client-sns");
let { PublishCommand } = require("@aws-sdk/client-sns");
// Set the AWS Region.
const REGION = "ap-northeast-1"; //e.g. "us-east-1"
// Create SNS service object.
const snsClient = new SNSClient({ region: REGION });
// Set the parameters
const params = {
Message: "test please" /* required */,
PhoneNumber: "+821012345678", //PHONE_NUMBER, in the E.164 phone number structure
};
const run = async () => {
try {
const data = await snsClient.send(new PublishCommand(params));
console.log("Success.", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err.stack);
}
};
run();
- 这是JS的回应
Success. {
'$metadata': {
httpStatusCode: 200,
requestId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
MessageId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
SequenceNumber: undefined
}
1条答案
按热度按时间1qczuiv01#
请先检查电话号码。您的密码似乎正确。
AWS SNS SMS遵循E.164格式。
接收邮件的电话号码。必须采用E.164格式。例如,美国电话号码可能为+12065550101
你可以阅读更多here
来自以上链接:
发送SMS消息时,请使用E.164格式指定电话号码。E.164是用于国际电信的电话号码结构标准。遵循此格式的电话号码最多可包含15位数字,并且前面带有加号字符(+)和国家/地区代码。例如,E.164格式的美国电话号码将显示为+1XXX5550100。