如何使用Twilio SMS / Voice api实现一次性验证(OTP)

时间:2015-07-08 08:06:51

标签: javascript spring-mvc authentication twilio

我可以使用twilio SMS APi发送短信

Map<String,String> params = new HashMap<String,String>();
params.put("From", fromNumber);
params.put("To", toNumber);
params.put("Body", "Bad news " + admins.get(toNumber) + ", take this");

try {
    // send an sms a call
    // ( This makes a POST request to the SMS/Messages resource)
    Sms sms = smsFactory.create(params);
    System.out.println("Success sending SMS: " + sms.getSid());
}

并希望在基于SPring MVC的Web应用程序中实现基于一次性密码(OTP)的身份验证。基于百里香叶的矿视图。我得到了这个链接,但这个实现是用PHP。https://www.twilio.com/docs/howto/two-factor-authentication

还尝试使用Node.js实现(https://www.twilio.com/docs/node/install#using)in此案例脚本已执行但未发送短信

<script>
// Your accountSid and authToken from twilio.com/user/account
var accountSid = 'ACabdb218474d12644b16a5cfe891ce962';
var authToken = 'a4ba7b6ec6eb09aeccfbbb2143bc951d';
var client = require('twilio')(accountSid, authToken);

client.messages.create({
        to: '+918800989508',
        from:'+1 415-099-2671',
        body: 'Jenny please?! I love you 3',

    }, function(err, message) {
        process.stdout.write(message.sid);
    });

</script>

基于Java的应用程序的任何实现。

1 个答案:

答案 0 :(得分:0)

来自Twilio的Ricky。

我们有一个更新的教程,展示了如何使用Java实现它:

https://www.twilio.com/docs/tutorials/walkthrough/two-factor-authentication/java/servlets

我们正在使用Authy验证用户,这是处理此问题的代码:

private Result sendApprovalRequest(User user) throws IOException {
    Parameters parameters = Parameters.builder()
            .addDetail("email", user.getEmail())
            .build();

    return client.sendApprovalRequest(
            user.getAuthyId(), "Request login to Twilio demo app", parameters);
}