2

I integrated omniauth-facebook using https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview. But I am getting error of :

Could not authenticate you from Facebook because "Invalid credentials".

I have devise installed. When i click on facebook sign in link, it redirects me to facebook login. When i enter my details, it comes back to devise sign and gives above error. I checked the solution for "Invalid credentials" on https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview and my app is header set for App Type = Web. Not getting why it is not working.

Also my app is live but yet to approve from facebook. But i don't think it is related to this error. Following are the things i did for omniauth-facebook:

Gemfile contains:

gem 'omniauth'
gem 'omniauth-facebook', '1.4.0'

In user model, added:

devise :omniauthable, :omniauth_providers => [:facebook]
attr_accessible :provider, :uid

  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => 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

devise.rb

require "omniauth-facebook"
config.omniauth :facebook, "APP_ID", "APP_SECRET", :scope => "offline_access, email" 

Link for facebook sign_in:

<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

route.rb:

devise_for :user, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

Omniauth controller:

class Users::OmniauthCallbacksController < 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 => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
   end
 end

Can anybody help in this?

Gaurav Sharma
  • 477
  • 9
  • 24
user2206724
  • 1,265
  • 3
  • 20
  • 38
  • I'm not sure about it, but might it be that you have a space before email on your omniauth scope? I had the same error due to a typo – golfadas Aug 21 '13 at 13:33
  • possible duplicate of [Rails: Could not authenticate you from Facebook because "Invalid credentials"](http://stackoverflow.com/questions/16176208/rails-could-not-authenticate-you-from-facebook-because-invalid-credentials) – mindriot Sep 02 '14 at 08:08
  • This has been flagged as a duplicate of an identical question you posted that had more info. In the future if you have additional info to add to a question, please edit the existing question instead of starting a new one. – mindriot Sep 02 '14 at 08:17

1 Answers1

1

Try to clear your browser cookies. sometimes omniauth won't work just because you have to delete your browser cookies.

an other thing could be that the version isn't right.

The problem seems to be the dependencies. At 1.4.0 it requires omniauth-oauth2 1.0.3, and at 1.4.1 it required omniauth-oauth 1.1.x

as seen here: https://github.com/intridea/omniauth/issues/276

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • versions are omniauth-oauth2 (1.0.2) for omniauth-facebook (1.4.0). Is it ok or it should be 1.0.3? – user2206724 Apr 23 '13 at 07:28
  • just try to update all as high as possible. that could work. but i'm not sure. – Kees Sonnema Apr 23 '13 at 07:29
  • cleared the cookies and I tried to upgrade using sudo gem install omniauth-oauth2 --version 1.0.3. But it is still 1.0.2. Getting same error. – user2206724 Apr 23 '13 at 08:45
  • the newest version of omniauth is 1.1.4, put this in your gemfile: gem "omniauth", "~> 1.1.4" and run bundle install. – Kees Sonnema Apr 23 '13 at 09:24
  • the newest version of omniauth-oauth is 1.0.1 put this in your gemfile: gem "omniauth-oauth", "~> 1.0.1" and run bundle install. – Kees Sonnema Apr 23 '13 at 09:25
  • gem "omniauth-oauth", "~> 1.0.1" or gem "omniauth-oauth2", "~> 1.0.1" – user2206724 Apr 23 '13 at 09:39
  • sorry forget those. i missed the 2. put this in your gemfile: gem "omniauth-oauth2", "~> 1.1.1" that's the current version :D that should work. – Kees Sonnema Apr 23 '13 at 09:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28719/discussion-between-user2206724-and-kees-sonnema) – user2206724 Apr 23 '13 at 09:42
  • (facebook) Callback phase initiated. (facebook) Authentication failure! invalid_credentials: OAuth2::Error, : {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}} Processing by OmniauthCallbacksController#failure – user2206724 Apr 23 '13 at 09:44