title: Omniauth-Twitter/Devise error: "Could not authenticate you from Twitter because "Invalid credentials""
body: <p>I'm using the omniauth-twitter gem with devise, and followed the <a href="http://railscasts.com/episodes/235-devise-and-omniauth-revised" rel="nofollow">railscast</a> on how to implement it.</p>
  
  <p>Everything seems to work fine - I can click on the sign_up with twitter link, it redirects me to twitter, I click on 'log in' and then it redirects me back BUT with the error message "Could not authenticate you from Twitter because "Invalid credentials".</p>
  
  <p>I'm at a total loss and the obvious solutions like mixing up the position of my consumer and secret key I have checked, and they're all fine.</p>
  
  <p>I tried reading the several different variations on SO regarding omniauth-devise and none of them helped.</p>
  
  <p>Here's the relevant code:</p>
  
  <pre><code> devise_scope :user do
      get 'sign_up', to: 'members#new', controllers: {registrations: "members"}
      get 'partners/sign_up', to: 'content_partners#new', controllers: {registrations: "content_partners"}
    end
    devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
  </code></pre>
  
  <p>From the config/initializers/device file:</p>
  
  <pre><code> require "omniauth-twitter"
     config.omniauth :twitter, 'CONSUMER_KEY', 'CONSUMER_SECRET'
  </code></pre>
  
  <p>My gemfile:</p>
  
  <pre><code>gem "omniauth", "~&gt; 1.1.1"
  gem "omniauth-twitter", "~&gt; 0.0.14"
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>class OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def all
      user = User.from_omniauth(request.env["omniauth.auth"])
      if user.persisted?
        flash.notice = "Signed in!"
        sign_in_and_redirect user
      else
        session["devise.user_attributes"] = user.attributes
        redirect_to sign_up_path
      end
    end
    alias_method :twitter, :all
  end
  </code></pre>
  
  <p>My user model:</p>
  
  <pre><code>  def self.from_omniauth(auth)
      where(auth.slice(:provider, :uid)).first_or_create do |user|
        user.provider = auth.provider
        user.uid = auth.uid
      end
    end
  
    def self.new_with_session(params, session)
      if session["devise.user_attributes"]
        new(session["devise.user_attributes"], without_protection: true) do |user|
          user.attributes = params
          user.valid?
        end
      else
        super
      end
    end
  </code></pre>
  
  <p>The link in my sign_up view file:</p>
  
  <pre><code>&lt;%= link_to "or Sign up with Twitter", user_omniauth_authorize_path(:twitter) %&gt;
  </code></pre>
  
  <p>Finally, here is what I'm getting from the logger:</p>
  
  <pre><code>Started GET "/users/auth/twitter" for 127.0.0.1 at 2012-12-11 15:54:01 +0000
  (twitter) Callback phase initiated.
  
  
  Started GET "/users/auth/twitter/callback?oauth_token=****blanked****&amp;oauth_verifier=****blanked****" for 127.0.0.1 at 2012-12-11 15:54:03 +0000
  Processing by OmniauthCallbacksController#twitter as HTML
    Parameters: {"oauth_token"=&gt;"****blanked****", "oauth_verifier"=&gt;"****blanked****"}
    User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."provider" = 'twitter' AND "users"."uid" = '803184474' LIMIT 1
     (0.1ms)  BEGIN
    User Exists (0.4ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT 1
     (0.2ms)  ROLLBACK
  Redirected to http://localhost:3000/sign_up
  Completed 302 Found in 8ms (ActiveRecord: 1.3ms)
  </code></pre>
  
  <p>Could anyone help me with this?!</p>