Ruby on rails Twilio Ruby验证失败

Ruby on rails Twilio Ruby验证失败,ruby-on-rails,ruby,ruby-on-rails-3,twilio,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 th

我使用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
class CallsControllerresponse.text
结束
结束
结束

这里是Twilio福音传道者

很难说为什么这对您不起作用——但我已经能够在您的代码中复制相同的问题。但我不确定是不是同样的原因。如果读取,则URL将包含在安全哈希中以验证请求

我正在使用将流量定向到我的应用程序,该应用程序正在从端口80转发到端口3000。我的Twilio webhook是
http://example.com/voice
,但由于此服务器设置,我的应用程序接收到的URL为
http://example.com:3000/voice

您可以通过从URL中删除端口号以使其与Twilio请求匹配来解决此问题(还有其他方法,例如将URL硬编码到验证中也可以):

我不能确定这是否正是你的问题,所以如果这没有帮助,你能发布更多关于传入请求参数等的信息吗?不过,请注意不要发布您的实际URL或Twilio凭据


希望这能有所帮助。

如果有人从那时起出现问题,那么解决办法是rails的参数不能工作/与twilio的参数不匹配

添加这一行并将其放入validate函数,它应该可以工作

 twilio_params = params.reject {|k,v| k.downcase == k}

虽然对某些请求使用twilio|u params=params.reject{k,v|k.downcase==k}将起作用,但有时twilio会在POST请求中包含非大写参数(例如,在聚集响应后发送数字时)。我发现
request.POST
env['rack.request.form\u 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
class CallsControllerresponse.text
结束
结束
结束

request.POST
也适用于我们,但使用控制器上可用的默认
params
方法验证失败。我们不需要对参数进行筛选,以查找下壳键。
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