1

I'm using Omniauth-Facebook to create and authenticate User. It works for the two first users, but fails for the third one. I still can authenticate; it's the CREATE process which fails. This appears to be a common issue without a solution.

error.log:

 facebook) Callback phase initiated.
(facebook) Authentication failure! invalid_credentials: OAuth2::Error, : 
{"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}

onmiauth.rb:

 Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :facebook, 'xxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
           :scope => 'email,user_birthday,user_hometown,user_location', :display => 'popup'
end

SessionsController:

class SessionsController < ApplicationController
  def create
      auth = request.env["omniauth.auth"]
      player = Player.find_by_provider_and_uid(auth["provider"], auth["uid"]) || Player.create_with_omniauth(auth)
      session[:player_id] = player.id
      redirect_to bienvenue_index_path, :notice => "Signed in!"
  end

  def destroy
  session[:player_id] = nil
  redirect_to root_url, :notice => "Signed out!"
  end
end

And my Player Model :

class Player < ActiveRecord::Base



  attr_accessible :email, :gender, :habitation, :image, :nom, :nom_complet, :prenom, :provider, :token, :uid, :taille, :poids, :pied, :poste_prefere, :vehicule
  has_many :matches
  has_many :activities

  validates_presence_of :prenom
  validates_presence_of :nom

  def self.create_with_omniauth(auth)

    large = "http://graph.facebook.com/#{auth["uid"]}/picture?type=large"

      create! do |player|
        player.provider = auth["provider"]
        player.uid = auth["uid"]
        player.nom_complet = auth["info"]["name"]
        player.nom = auth["info"]["last_name"]
        player.prenom = auth["info"]["first_name"]
        player.image = auth["info"]["image"]
        player.image_large = large
        player.email = auth["info"]["email"]
        player.gender = auth["extra"]["raw_info"]["gender"]
        player.habitation = auth["extra"]["raw_info"]["location"]["name"]          
        player.token = auth["credentials"]["token"]

      end
    end 

end
JoshDM
  • 4,939
  • 7
  • 43
  • 72
GrégoireC
  • 279
  • 5
  • 15

2 Answers2

3

For other people who stumble over this same error mine was caused by another issue.

I fixed it by place skip_before_filter :authenticate in the Users::OmniauthCallbacksController

Why I think this worked:

Initially I followed the the trail to this bug in the omniauth_facebook gem.

This highlighted the fact that the error message relates to the fact that two FB callback requests are being sent in quick succession. Unfortunately, I didn't have the omniauth details defined twice so initially couldn't work out where that was coming from.

Then I realised that in my ApplicationController I have before_filter :authenticate. I think what was happening was that the Facebook callback request was hitting this authentication barrier and then immediately request a new authentication.

Telling Devise to skip this action resolved the issue for me.

Betjamin Richards
  • 1,071
  • 2
  • 12
  • 34
1

Ok, I found the solution.

In my model player.rb this line :

player.habitation = auth["extra"]["raw_info"]["location"]["name"]

returned Nil for one player.

Comment this line fixed the problem. Hope this helps

GrégoireC
  • 279
  • 5
  • 15