Steven Aguilar
Steven Aguilar

Reputation: 3049

ArgumentError (Account SID and auth token are required):

I am getting the following error when I input a phone number into a form field

ArgumentError in Apps::TextyController#send_text

Account SID and auth token are required

But I have the Auth token and the Account SID in the code.

module Messenger

  def send_sms(number)
    acct_sid = ENV['AC0b06exxxxxxxxxxxxx']
    auth_token = ENV['68ce616xxxxxxxxxxx']

     @client = Twilio::REST::Client.new acct_sid, auth_token

    from = '+6466817345'

    message = @client.account.messages.create(
        :from => from,
        :to => '+1'+number,
        :body => 'accept my application'
        )
  end

end

I am using an older Ruby -v 2.2.0 and Rails 4.

This is texty_controller.rb

class Apps::TextyController < ApplicationController

  def index
    @phone = Phone.new
  end

  def send_text
    @phone = Phone.new(phone_params)
    @phone.send_sms(@phone.clean_number)
    @phone.save
    redirect_to :back
  end

  private

  def phone_params
    params.require(:phone).permit(:number)
  end

end

which is a clone from the following website

Upvotes: 1

Views: 907

Answers (2)

Steven Aguilar
Steven Aguilar

Reputation: 3049

I fixed this issue by doing the following steps.

1) I modified the messenger.rb file by saving the acct_sid and auth_token the following way:

acct_sid = 'AC0b06exxxxxxxxxxxxx'

auth_token = '68ce616xxxxxxxxxxx

2) then after saving the file I went on terminal and export the enviroment variables from there the following way:

export TWILIO_AUTH_TOKEN=AC0b06exxxxxxxxxxxxx

export TWILIO_ACCOUNT_SID=68ce616xxxxxxxxxxx

3) In step three I go to the messenger.rb file and modified the acct_sid and auth_token the following way.

acct_sid = ENV['TWILIO_ACCOUNT_SID']

auth_token = ENV['TWILIO_AUTH_TOKEN']

Make sure that your using TWILIO live credentials. Test credentials are limited to three resources.

Upvotes: 1

cdimitroulas
cdimitroulas

Reputation: 2539

How have you stored the SID and Auth Token as environment variables? The norm is to save them in an application.yml file and name them something like TWILIO_AUTH_TOKEN = 'AUSHXU******' TWILIO_ACCOUNT_SID = 'SUHXB823*****'

and then call them using ENV['TWILIO_AUTH_TOKEN'].

Have you tried calling your environment variables in the console? If you get nil that means that you are not accessing them correctly and thus your acct_sid and auth_token local variables will be nil.

Upvotes: 2

Related Questions