title: Rails: Could not authenticate you from Facebook because "Invalid credentials"
body: <p>I integrated omniauth-facebook using <a href="https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview" rel="noreferrer">https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview</a>. But I am getting error of :</p>
  
  <pre><code>Could not authenticate you from Facebook because "Invalid credentials".
  </code></pre>
  
  <p>And in logs, getting this: </p>
  
  <pre><code>Authentication failure! invalid_credentials: OAuth2::Error, : {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}
  </code></pre>
  
  <p>I have devise installed. When i click on facebook sign in link, it comes back to devise sign "www.mealnut.com/user/sign_in#<em>=</em>" and gives above error. I checked the solution for "Invalid credentials" on <a href="https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview" rel="noreferrer">https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview</a> and as mentioned there, my app is header set for App Type = Web. Not getting why it is not working.</p>
  
  <p>Also my app is pending review from facebook. But i don't think it is related to this error. Following are the things i did for omniauth-facebook:</p>
  
  <p>Gemfile contains:</p>
  
  <pre><code>gem "omniauth", "~&gt; 1.1.4"
  gem 'omniauth-facebook', '1.4.1'
  </code></pre>
  
  <p>In user model, added:</p>
  
  <pre><code>devise :omniauthable, :omniauth_providers =&gt; [:facebook]
  attr_accessible :provider, :uid
  
    def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
      user = User.where(:provider =&gt; auth.provider, :uid =&gt; auth.uid).first
      unless user
      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]
                        )
    end
  user
  end
  </code></pre>
  
  <p>devise.rb</p>
  
  <pre><code>require "omniauth-facebook"
  config.omniauth :facebook, "APP_ID", "APP_SECRET", :scope =&gt; "offline_access, email" 
  </code></pre>
  
  <p>omniauth.rb:</p>
  
  <pre><code>OmniAuth.config.logger = Rails.logger
  Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], {:provider_ignores_state =&gt; true}
  end
  </code></pre>
  
  <p>route.rb:</p>
  
  <p>devise_for :user, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }</p>
  
  <p>Omniauth controller:</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def facebook
      @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
  
      if @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
        session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  end
  </code></pre>
  
  <p>Can anybody help in this?</p>