通过本课程构建ROR应用程序并发送twilio消息来验证人员的手机号码。我收到错误“请求的资源/2010-04-01/Accounts//Messages.json未找到”发现上一个问题请求,但他们没有太多的代码要审查,他提到他最终有它工作提到他有一个$ sid和token的null变量;不知道要验证什么。我从我的帐户尝试了测试和生产sid和令牌#s。非常感谢你的关注。
initailizer / twilio.rb
Twilio.configure do |config|
config.account_sid = ENV['TWILIO_ACCOUNT_SID']
config.auth_token = ENV['TWILIO_AUTH_TOKEN']
end
模型/ user.rb
def generate_pin
self.pin = SecureRandom.hex(2)
self.phone_verified = false
save
end
def send_pin
@client = Twilio::REST::Client.new
@client.messages.create(
from: '+15812345678',
to: self.phone_number,
body: "Your pin is #{self.pin}"
)
end
def verify_pin(entered_pin)
update(phone_verified: true) if self.pin == entered_pin
end
Application.yml
TWILIO_ACCOUNT_SID: '12345678901234567'
TWILIO_AUTH_TOKEN: '123434567890123456'
TWILIO_NUMBER: '+15878551234'
用户控制器
def update_phone_number
current_user.update_attributes(user_params)
current_user.generate_pin
current_user.send_pin
redirect_to edit_user_registration_path, notice: "Saved!"
rescue Exception => e
redirect_to edit_user_registration_path, alert: "#{e.message}"
end
def verify_phone_number
current_user.verify_pin(params[:user][:pin])
if current_user.phone_verified
flash[:notice] = "FYi Your phone number is verified"
else
flash[:alert] = "Cannot verify your phone number"
end
redirect_to edit_user_registration_path
rescue Exception => e
redirect_to edit_user_registration_path, alert: "#{e.message}"
end
private
def user_params
params.require(:user).permit(:phone_number, :pin)
end
答案 0 :(得分:1)
我不确定,但你可以试试吗
def send_pin
account_sid = 'your-account-sid'
auth_token = 'your-auth-token'
@client = Twilio::REST::Client.new account_sid, auth_token
@client.messages.create({
from: '+15812345678',
to: self.phone_number,
body: "Your pin is #{self.pin}"
})
end