Twitter gem requires authentication now

Unable to verify your credentials

At least consumer key and secret are required, and that’s easy to fix:

  1. Register your application with twitter

  2. Put consumer key and secret into environment variables:

     export CONSUMER_KEY=123
     export CONSUMER_SECRET=secret

    or use GitHub - bkeepers/dotenv: A Ruby gem to load environment variables from `.env`. instead

  3. Add the following to config/initializers/twitter.rb:

     Twitter.configure do |config|
       config.consumer_key = ENV['CONSUMER_KEY']
       config.consumer_secret = ENV['CONSUMER_SECRET']

Happy Testing


Thanks for posting this!

Please note that this is not working with newer version of twitter gem (version 5), so make sure to use 4.8.1 and it will work.

Also at first glance it looks that twitter gem API has been changed, so make sure to use version 4.8.1 if you are working on TDD workshop.

1 Like

Hi wik, thanks for your post. I’m doing the TDD workshop and I’m stuck on this Twitter::Error::Forbidden error

Someting is not clear to me, in what file do I need to put those environment variables?

Thanks for your help,


solved it with the .dotenv gem

just type the following within your current bash session before running the specs:

export CONSUMER_KEY=123
export CONSUMER_SECRET=secret

or prepend to the actual command, e.g.:

CONSUMER_KEY=123 CONSUMER_SECRET=secret bundle exec rails s

But yeah, dotenv is a solid solution

1 Like

Thanks very much. Your suggestions worked for me, even rather quickly.
Ah, what a world: create a application authentication, hide it from the app, configure the gem.
Then, only then do we ge the tweets.
Oh well.

Second @patrikbona: The v5 of the twitter gem breaks almost everything that was working in v4.

It should be noted that the consumer_key and consumer_secret are called the API key and API secret in today’s (2014-06-14) version of creating your twitter application.