ruby-on-rails - Twilio-Ruby 验证失败

标签 ruby-on-rails ruby ruby-on-rails-3 twilio

我正在使用 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.POSTenv['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/

相关文章:

ruby Dir.glob 之谜 : Where are the FNM_xxx flags described?

ruby - 你如何模拟 RSpec 中的 break 语句?

ruby-on-rails - 从异常返回消息中删除 "Validation failed"消息

ruby-on-rails - 顶级 ruby​​onrails 截屏视频/教程/博客 url

ruby-on-rails - 过滤掉 poltergeist 中的警告

ruby-on-rails - 运行 sudo gitlab-ctl reconfigure 后服务器重启

mysql - 两个不同协会的 Rails 订单

mysql - 带有 mysql 数据库的炼油厂 cms

sql - 从 Active Record Relation 中移除对象而不删除它

ruby-on-rails - 在 "tmp:clear"之后运行任务