无法使用Twilio试用帐户接收短信

时间:2015-08-28 15:15:16

标签: ruby-on-rails ruby-on-rails-4 twilio

在此错误弹出之前几个小时后,一切都工作正常,现在我无法收到短信。以下是日志

Started POST "/phone_numbers" for 127.0.0.1 at 2015-08-28 20:24:38 +0530
Processing by PhoneNumbersController#create as JS
  Parameters: {"utf8"=>"✓", "phone_number"=>{"phone_number"=>"+919738437250", "user_id"=>"9"}, "commit"=>"Send PIN"}
  PhoneNumber Load (0.4ms)  SELECT  "phone_numbers".* FROM "phone_numbers" WHERE "phone_numbers"."phone_number" = $1 AND "phone_numbers"."user_id" = $2 LIMIT 1  [["phone_number", "+919738437250"], ["user_id", 9]]
   (0.1ms)  BEGIN
  SQL (0.2ms)  UPDATE "phone_numbers" SET "pin" = $1, "updated_at" = $2 WHERE "phone_numbers"."id" = $3  [["pin", "4143"], ["updated_at", "2015-08-28 14:54:38.800401"], ["id", 51]]
   (99.4ms)  COMMIT
Completed 500 Internal Server Error in 4519ms (ActiveRecord: 100.1ms)

Twilio::REST::RequestError - The requested resource /2010-04-01/Accounts//Messages.json was not found:
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:124:in `connect_and_send'
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/base_client.rb:54:in `block (2 levels) in <class:BaseClient>'
  twilio-ruby (4.2.1) lib/twilio-ruby/rest/list_resource.rb:95:in `create'
   () home/pavan/Roorah/app/controllers/phone_numbers_controller.rb:14:in `create'

以下是我的代码。

#config/initializers/twilio.rb
path = File.join(Rails.root, "config/twilio.yml")
TWILIO_CONFIG = YAML.load(File.read(path))[Rails.env] || {'sid' => '', 'from' => '', 'token' => ''}

#config/twilio.yml
development:
from: '+1xxxxxxxxxx'
sid: 'Axxxxxxxxxxxxxxxxxxxxxxxxxe0'
token: '5xxxxxxxxxxxxxxxxxxxxxxxxxa0'

#app/controllers/phone_numbers_controller.rb
class PhoneNumbersController < ApplicationController

  def new
    @phone_number = PhoneNumber.new
  end

  def create
    @phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number], user_id: params[:phone_number][:user_id])
    @phone_number.generate_pin
  # Instantiate a Twilio client
  client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])

    # Create and send an SMS message
    client.messages.create(
      from: TWILIO_CONFIG['from'],
      to: @phone_number.phone_number,
      body: "Enter #{@phone_number.pin} to verify your phone number"
      )
    respond_to do |format|
    format.js # render app/views/phone_numbers/create.js.erb
  end
end

def verify
  @phone_number = PhoneNumber.find_by(phone_number: params[:hidden_phone_number])
  @phone_number.verify(params[:pin])
  @user = User.find(params[:user_id])
  @user.update(primary_phone: params[:hidden_phone_number], verified: true) if @phone_number.verify(params[:pin])
  respond_to do |format|
    format.js
  end
end
end

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

看起来您使用YAML加载配置的位置,帐户SID将回退到默认值,空字符串。这就是为什么堆栈跟踪显示的URL没有您的帐户SID:

Twilio :: REST :: RequestError - 找不到请求的资源 /2010-04-01/Accounts//Messages.json

检查您的配置以及您如何加载YAML以及TWILIO_CONFIG['sid']的值是什么。

此外,加载配置的更简单方法可能是使用内置config_for帮助程序的Rails。在这里查看文档:{​​{3}}