title: facebook omniauth login with rails/devise not working because of error validating authentication code
body: <p>I am trying to allow my users to login using facebook into a rails application.  Here is the "sign up with fb link":</p>
  
  <pre><code>&lt;%= link_to "Sign in with FB", omniauth_authorize_path(resource_name, provider) %&gt;
  </code></pre>
  
  <p>That generates the following html:</p>
  
  <pre><code>&lt;a href="/users/auth/facebook"&gt;Sign in with Facebook&lt;/a&gt;
  </code></pre>
  
  <p>That should take me to this controller:</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
  
    before_action :test_if_in_right_controller
  
    def facebook
      puts "in the fb controller"
      # Attempt to find the User
      @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
      puts "1.  in fb.  user is #{@user.inspect}"
      if @user.persisted?
        puts "2. user persisted"
        sign_in_and_redirect @user, :event =&gt; :authentication # This will throw if @user is not activated
        set_flash_message(:notice, :success, :kind =&gt; "Facebook") if is_navigational_format?
      else
        puts "3.  in else statement.  user did not persist."
        #session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  
    def passthru
      puts "in the passthru"
      render :file =&gt; "#{Rails.root}/public/404.html", :status =&gt; 404, :layout =&gt; false
    end
  
    def test_if_in_right_controller
      puts "in the before action"
    end
  
  end
  </code></pre>
  
  <p><strong>Heres the issue:</strong></p>
  
  <p>The before_action is working because the <code>puts "in the before action"</code> line appears in the logs.  But then none of the other <code>puts</code> in the controller are working.  What's really weird is that facebook is actually being contacted because I get the following error:</p>
  
  <pre><code>E, [2015-11-04T08:44:36.357124 #1049] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: OAuth2::Error, : 
  {"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100,"fbtrace_id":"DAgHQ3MuN59"}}
  </code></pre>
  
  <p><strong>Here are the full server logs</strong></p>
  
  <pre><code>Started GET "/users/auth/facebook/callback?code=AQA-MlSTW1tu9NuiaRa73XpD2nJKOgEmdFjFq3dZMC0m48Eep04dbql-2I80Up9CBYTAD_D7NRG_sVLVjMWRlvDtWirDhZuv1XdVZQti3I-ljSYSoiXhcYsleIZnK9osNkuYAFhF40mqM44-fZAiAGME2ny2ISj0XQIw4vvJvQDUkhS1W1jssMmCJfJM1gLbsyjgz3X3kgMRMS6_i-DTreS-lQLamfL5Kv3ElUenLTlZsmNwb3aTzcFtIV6X_5-c5XrVEs-VMmNdtEPiUFccAa6aOfwzPeP_iEyWz2F38KlKgCpOVapLVzH_fjx_LN0XOTRwJxreDe66gDP1MbgRhGMiahxQaaGEDxgxDYy3bxKW-g&amp;state=3de1318e2031f74043cb6bd387b1eda3b3ba244d749993c5" for 127.0.0.1 at 2015-11-04 08:44:36 -0500
  I, [2015-11-04T08:44:36.141145 #1049]  INFO -- omniauth: (facebook) Callback phase initiated.
  E, [2015-11-04T08:44:36.357124 #1049] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: OAuth2::Error, : 
  {"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100,"fbtrace_id":"DAgHQ3MuN59"}}
  Processing by Users::OmniauthCallbacksController#failure as HTML
    Parameters: {"code"=&gt;"AQA-MlSTW1tu9NuiaRa73XpD2nJKOgEmdFjFq3dZMC0m48Eep04dbql-2I80Up9CBYTAD_D7NRG_sVLVjMWRlvDtWirDhZuv1XdVZQti3I-ljSYSoiXhcYsleIZnK9osNkuYAFhF40mqM44-fZAiAGME2ny2ISj0XQIw4vvJvQDUkhS1W1jssMmCJfJM1gLbsyjgz3X3kgMRMS6_i-DTreS-lQLamfL5Kv3ElUenLTlZsmNwb3aTzcFtIV6X_5-c5XrVEs-VMmNdtEPiUFccAa6aOfwzPeP_iEyWz2F38KlKgCpOVapLVzH_fjx_LN0XOTRwJxreDe66gDP1MbgRhGMiahxQaaGEDxgxDYy3bxKW-g", "state"=&gt;"3de1318e2031f74043cb6bd387b1eda3b3ba244d749993c5"}
  in the before action
  Redirected to http://localhost:3000/login
  Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
  </code></pre>
  
  <p><strong>Here is the <code>User.find_for_facebook_oauth</code> method in the facebook method from the controller</strong></p>
  
  <pre><code>def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    puts "in the fb model method".green
    user = User.where(provider: auth.provider, uid: auth.uid).first
    puts "1.  user is #{user.inspect}".green
    # The User was found in our database
    return user if user
  
    # Check if the User is already registered without Facebook
    user = User.where(email: auth.info.email).first
    puts "2.  user is already registered without fb.  #{user.inspect}".green
    return user if user
  
    # The User was not found and we need to create them
    new_user = User.create(name:     auth.extra.raw_info.name,
                provider: auth.provider,
                uid:      auth.uid,
                email:    auth.info.email,
                password: Devise.friendly_token[0,20])
  
    puts "the new user is #{new_user.inspect}".green
  end
  </code></pre>
  
  <p>In my config/intitializers/devise.rb file, I have:</p>
  
  <pre><code>config.omniauth :facebook, ENV['APP_KEY'], ENV['FACEBOOK_SECRET_ID']
  </code></pre>