Why am I receiving “This authorization code has been used” after the first 2 users?

故事扮演 提交于 2019-12-11 11:53:47

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14662454/why-am-i-receiving-this-authorization-code-has-been-used-after-the-first-2-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!