【问题标题】:Facebook authentication isn't working using devise+omniauth facebook(except Admin user or test user)Facebook 身份验证无法使用 devise+omniauth facebook(管理员用户或测试用户除外)
【发布时间】:2014-10-21 13:24:45
【问题描述】:

我在我的 rails 4.1 应用程序中使用 devise 和 omniauth facebook 进行用户身份验证。用户身份验证与 facebook 的管理员用户和测试用户完美配合。但问题是当我尝试使用其他 facebook 用户登录时,无法创建新用户并将我重定向到 localhost:3000/users/sign_up#=。我也在heroku上传了这个项目,但我遇到了同样的问题。

这里是 devise.rb

require "omniauth-facebook"
config.omniauth :facebook, "App_ID", "App_Secret"

route.rb 文件:

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

User.rb 模型:

class User < ActiveRecord::Base

  has_many :users
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    if user
      return user
    else
      registered_user = User.where(:email => auth.info.email).first
      if registered_user
        return registered_user
      else
        user = User.create(name:auth.extra.raw_info.name,
                            provider:auth.provider,
                            uid:auth.uid,
                            email:auth.info.email,
                            first_name:auth.info.first_name,
                            last_name:auth.info.last_name,
                            image:auth.info.image,
                            location:auth.extra.raw_info.location,
                            gender:auth.extra.raw_info.gender,
                            oauth_token:auth.credentials.token,
                            password:Devise.friendly_token[0,20],
                          )
      end

    end
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
  end
end

Omniauth_call_back_controller.rb

class 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

查看文件:

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

【问题讨论】:

  • 您请求哪些权限?
  • 我拥有 public_profile、email、user_birthday、user_education_history 和 user_birthday 的权限。

标签: facebook ruby-on-rails-4 heroku devise omniauth


【解决方案1】:

我猜这行不通,因为当您想对应用程序的管理员/开发人员/测试人员以外的其他用户使用扩展权限时,您需要通过 FB 对您的应用程序的审核

查看https://developers.facebook.com/docs/apps/review/login#do-you-need-review的文档

【讨论】:

  • 感谢您的回复。我已经通过了fb的许可。
  • 我也在 fb 的“状态和评论”菜单中完成了这项工作。
猜你喜欢
  • 2013-02-04
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多