我正在使用 twilio-ruby gem 来验证请求是否来自 twilio,但它总是错误地使 twilio 请求无效。这是我到目前为止所拥有的
class CallsController < ApplicationController
before_filter :authenticate_request
private
# This action validates that the request are coming from twilio. It uses the twilio-ruby gem
# to validate that the twilio signature, url, and params are correctly from twilio
def authenticate_request
twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE']
validator = Twilio::Util::RequestValidator.new(ENV['TWILIO_AUTH'])
verified = validator.validate(request.url, params, twilio_signature)
unless verified
response = Twilio::TwiML::Response.new do |r|
r.Say 'Unvalidated request'
r.Hangup
end
render :xml => response.text
end
end
end
最佳答案
使用 twilio_params = params.reject {|k,v| 时k.downcase == k} 适用于某些请求,有时 Twilio 确实在 POST 请求中包含非大写的参数(例如,在 Gather 响应后发送数字时)。我发现 request.POST
或 env['rack.request.form_hash']
对我有用:
所以,在上下文中:
class CallsController < ApplicationController
before_filter :authenticate_request
private
# This action validates that the request are coming from twilio. It uses the twilio-ruby gem
# to validate that the twilio signature, url, and params are correctly from twilio
def authenticate_request
twilio_signature = request.headers['HTTP_X_TWILIO_SIGNATURE']
twilio_params = request.POST
validator = Twilio::Util::RequestValidator.new(ENV['TWILIO_AUTH'])
verified = validator.validate(request.url, twilio_params, twilio_signature)
unless verified
response = Twilio::TwiML::Response.new do |r|
r.Say 'Unvalidated request'
r.Hangup
end
render :xml => response.text
end
end
end
关于ruby-on-rails - Twilio-Ruby 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24154840/