1

我正在使用 Omniauth-Facebook 来创建和验证用户。它适用于前两个用户,但不适用于第三个用户。我仍然可以进行身份​​验证;这是失败的 CREATE 过程。这似乎是一个没有解决方案的常见问题。

错误日志:

 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

会话控制器:

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

还有我的播放器模型:

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
4

2 回答 2

3

对于其他偶然发现同样错误的人来说,我的错误是由另一个问题引起的。

skip_before_filter :authenticateUsers::OmniauthCallbacksController

为什么我认为这有效:

最初,我在omniauth_facebook gem 中跟踪了这个错误。

这突出了错误消息与快速连续发送两个 FB 回调请求这一事实有关的事实。不幸的是,我没有定义两次omniauth详细信息,所以最初无法确定它的来源。

然后我意识到在我的ApplicationController我有before_filter :authenticate。我认为发生的事情是 Facebook 回调请求遇到了这个身份验证障碍,然后立即请求新的身份验证。

告诉 Devise 跳过此操作为我解决了这个问题。

于 2013-05-31T14:12:33.090 回答
1

好的,我找到了解决方案。

在我的模型中player.rb,这条线:

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

为一名玩家返回 Nil。

评论这一行解决了这个问题。希望这可以帮助

于 2013-02-02T16:20:20.107 回答