为什么我在前2位用户之后收到“此授权码已被使用”?

时间:2013-02-02 13:40:39

标签: ruby-on-rails facebook omniauth

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

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

我的玩家模型:

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

2 个答案:

答案 0 :(得分:3)

对于偶然发现同样错误的其他人,我的另一个问题也是如此。

我将地点skip_before_filter :authenticate修改为Users::OmniauthCallbacksController

为什么我认为这有效:

最初,我跟踪了omniauth_facebook gem中的bug

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

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

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

答案 1 :(得分:1)

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

在我的模型player.rb中这一行:

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

为一名球员返回Nil。

注释此行修复了问题。 希望这有帮助