Ruby on rails Rails设计了omniauth facebook。坚持吗?返回错误

Ruby on rails Rails设计了omniauth facebook。坚持吗?返回错误,ruby-on-rails,devise,omniauth,omniauth-facebook,Ruby On Rails,Devise,Omniauth,Omniauth Facebook,我花了最后一天的时间试图解决这个问题,这让我发疯 昨晚,我让facebook登录到我的网站,并正在检索基本的用户信息。当我将:scope=>“user_birth”添加到config/initializers/omniauth.rb时,我的问题就开始了,现在看起来像这样 Rails.application.config.middleware.use OmniAuth::Builder do provider :facebook, "APP_ID", "APP_SECRET", :scope

我花了最后一天的时间试图解决这个问题,这让我发疯

昨晚,我让facebook登录到我的网站,并正在检索基本的用户信息。当我将
:scope=>“user_birth”
添加到
config/initializers/omniauth.rb
时,我的问题就开始了,现在看起来像这样

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "APP_ID", "APP_SECRET", :scope => 'user_birthday'
end
作为参考,我已经从
config/initializers/designe.rb

我花了好几个小时试图让它发挥作用,但最终不得不放弃。然后今天早上我又运行了一次,它成功了。我欣喜若狂,试图在
:scope
中添加另一个参数,但现在整个过程又被破坏了。如果我删除
:scope
,我可以让它工作,但当我把它放回时,每次都会失败(即使它只是
:scope=>“用户生日”
,就像我今天早上第一件事就是工作一样)

为了找到问题,我将调试代码放入
omniauth\u callbacks\u controller.rb
中,它现在看起来像:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
        @user = User.from_omniauth(request.env["omniauth.auth"])
        puts "start before persist debug"
        puts @user.birthday
        puts @user.persisted?
        puts "end before persist debug"

    if @user.persisted?         
            puts "Start persisted debug"
            puts request.env["omniauth.auth"]
            puts "End debug"            
      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"]
            puts "Start unpersisted debug"
            puts request.env["omniauth.auth"]
            puts "End debug"
      redirect_to new_user_registration_url
    end
  end
end
我一辈子都搞不明白为什么
。persistend?
返回false。我使用Nitrous.io开发Heroku postgresql数据库。我已通过运行来确认数据库中没有用户

rails c
User.all
这将返回:

  User Load (89.4ms)  SELECT "users".* FROM "users"                                                                                                                                                                                                     
=> #<ActiveRecord::Relation []> 
我已经把每件事都复习了50遍,几乎要放弃了

我唯一能想到的是
where(provider:auth.provider,uid:auth.uid)
正在返回一些内容(因为我的数据库是空的,所以不应该返回)。是否可能有一个索引存在于我的数据库之外的某个地方,而这正是它正在搜索的

拜托,为了我的理智,有人能帮忙吗?如果你需要更多的信息,我很乐意提供

编辑1

刚刚尝试了以下方法,它的效果让我比以往任何时候都更困惑:

  • 从我的facebook帐户中删除应用程序,因为我正在使用该帐户进行测试
  • 请尝试使用facebook登录,并将
    :scope=>“用户生日”
    保留在中。Facebook将请求的权限列为
    您的公共档案和生日
    。接受并发送回我的网站,该网站根据上述失败(即使信息肯定会被发送回)
  • 删除
    :scope=>“用户生日”
    ,然后再次尝试使用facebook登录。直接访问facebook,该网站将请求的权限列为您的公共档案和电子邮件地址。接受并直接返回到网站,该网站现在可以正常工作,并且已经存储和访问了用户的生日信息,因为我获得了上面第2条facebook的许可

  • 我现在完全不知道该对象为什么没有被保存。你需要把错误放进去

    puts @user.errors.to_a
    
    并检查
    auth

    puts request.env["omniauth.auth"]
    

    我也遇到了同样的问题,按照上面的答案,我在代码上加了“@user.errors.to_yaml”,看看哪里出了错误,我找到了

    我也在使用“设计”和“omniauth facebook”。omniauth facebook的默认范围是“电子邮件”。但是,我将属性放在范围内:“用户关于我、用户状态、用户位置、用户生日、用户照片”。我需要在范围上添加“电子邮件”,以便在创建“用户”时使用。当我看到我的错误时,我发现了这一点:“电子邮件不要空白”

    总结:
    如果在作用域中插入属性,请始终也放置“电子邮件”。

    Facebook并不总是为用户返回电子邮件

    来自facebook开发者

    一些可能的原因:

    • 帐户上没有电子邮件地址
    • 帐户上没有确认的电子邮件地址
    • 帐户上没有已验证的电子邮件地址
    • 用户输入了一个安全检查点,要求他们重新确认 他们的电子邮件地址,但他们尚未这样做
    • 无法访问用户的电子邮件地址
    您还需要“电子邮件”扩展权限,即使对于文件中有有效、已确认、可访问电子邮件地址的用户也是如此

    用户输入了一个安全检查点,要求他们重新确认他们的电子邮件地址,但他们还没有这样做 无法访问用户的电子邮件地址

    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
        def facebook
            puts request.env["omniauth.auth"]   #  check if request.env["omniauth.auth"] is provided an email
            if request.env["omniauth.auth"].info.email.present?
             @user = User.from_omniauth(request.env["omniauth.auth"])
                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
            else
              redirect_to new_user_registration_url, notice: "Can't sign in to your #{request.env["omniauth.auth"].provider} account. Try to sign up."
            end
        end
      end
    
    class Users::OmniAuthCallbackController:authentication#如果未激活@user,将引发此错误
    如果是导航格式,设置flash消息(:notice,:success,:kind=>“Facebook”)?
    其他的
    会话[“designe.facebook_data”]=request.env[“omniauth.auth”]
    将\重定向到新\用户\注册\ url
    结束
    其他的
    重定向到新的用户注册url,注意:“无法登录到您的{request.env[“omniauth.auth”].provider}帐户。请尝试注册。”
    结束
    结束
    结束
    
    您的生日栏(在用户表中)的类型是什么?字符串。根据我的编辑,它在步骤3中保存并可访问。对于这个MohamedDia27,感谢您。我不知道如何调试.first\u或\u create()但是你的.errors.to\u a()做得很好。问题是,当我请求用户的生日时,facebook出于某种原因停止发送用户电子邮件,并抛出错误“电子邮件不能为空”。如果我还请求用户的生日,我假定我必须将它显式地添加到范围中。再次感谢。
    puts request.env["omniauth.auth"]
    
    class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
        def facebook
            puts request.env["omniauth.auth"]   #  check if request.env["omniauth.auth"] is provided an email
            if request.env["omniauth.auth"].info.email.present?
             @user = User.from_omniauth(request.env["omniauth.auth"])
                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
            else
              redirect_to new_user_registration_url, notice: "Can't sign in to your #{request.env["omniauth.auth"].provider} account. Try to sign up."
            end
        end
      end