title: Rails: OAuth::Unauthorized 401 Authorization Required using OmniAuth-Twitter
body: <p>I have implemented several different strategies found in StackOverFlow, but so far, none seem to affect the error being thrown:</p>
  
  <p><code>OAuth::Unauthorized</code>
  <code>401 Authorization Required</code></p>
  
  <p>I am following <a href="http://railscasts.com/episodes/241-simple-omniauth-revised?autoplay=true" rel="nofollow noreferrer">Ryan Bates' RC #241</a> and get to the point where I click "Sign-in with Twitter" and I get the error.  I went ahead and added the response route to the <code>routes.rb</code> file as listed here:</p>
  
  <p><strong><code>routes.rb</code>:</strong></p>
  
  <pre><code>match 'auth/twitter/callback', to: 'user#update'
  </code></pre>
  
  <p>thinking that the error might be caused from the callback function.  Same error.  A look at my <code>dev.log</code> shows this:</p>
  
  <pre><code>Started GET "/auth/twitter" for 127.0.0.1 at 2014-09-16 18:52:08 -0600
  (twitter) Request phase initiated.
  
  OAuth::Unauthorized (401 Authorization Required):
  oauth (0.4.7) lib/oauth/consumer.rb:216:in `token_request'
  oauth (0.4.7) lib/oauth/consumer.rb:136:in `get_request_token'
  omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:29:in `request_phase'
  omniauth-twitter (1.0.1) lib/omniauth/strategies/twitter.rb:60:in `request_phase'
  omniauth (1.2.2) lib/omniauth/strategy.rb:215:in `request_call'
  omniauth (1.2.2) lib/omniauth/strategy.rb:183:in `call!'
  omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
  ...
  script/rails:6:in `require'
  script/rails:6:in `&lt;top (required)&gt;'
  -e:1:in `load'
  -e:1:in `&lt;main&gt;'
  </code></pre>
  
  <p>So I know the issue is with the authentication with Twitter going out.  Must be the KEY and SECRET, right?</p>
  
  <p>Now, I have put the KEY and SECRET in as ENV[] variables, as direct strings to the <code>environment/development.rb</code> file, taken out the "ENV[]" variables, etc., as per suggestions found all over Stack.</p>
  
  <p>My KEY and SECRET now reside in a custom configuration as discussed <a href="https://stackoverflow.com/questions/11648620/setting-environment-variables-in-rails-3-devise-omniauth">here...</a></p>
  
  <p><strong><code>config/initializers/social_media.rb:</code></strong></p>
  
  <pre><code>TWITTER_CONFIG = YAML.load_file("#{::Rails.root}/config/twitter.yml")[::Rails.env]
  </code></pre>
  
  <p>The <code>config/initializers/omniauth.rb</code> file:</p>
  
  <pre><code>OmniAuth.config.logger = Rails.logger
  
  Rails.application.config.middleware.use OmniAuth::Builder do
    provider :twitter, TWITTER_CONFIG['app_id'], TWITTER_CONFIG['secret']
  end   
  </code></pre>
  
  <p>Any ideas on the <code>ActionController: Exception caught OAuth::Unauthorized - 401 Authorization Required</code>?  This is probably a Noob error, but my Google-Fu is just Google-F'ed right now...</p>