1

我的网页上有一个 Ruby on Rails facebook 登录设置,使用omniauth-facebook 1.4.1(不使用Devise)和facebook api 2.4。虽然登录有效,但我无法使用范围检索电子邮件或 user_birthday。我已经搜索了所有关于此的帖子,但对我来说仍然没有解决。

在我的初始化程序 /config/initializers/omniauth.rb 中,

Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'APP_ID', 'APP_SECRET', :scope => 'email', info_fields: 'email,name', :display => 'popup'
end

在我的 /models/user.rb 中,

class User < ActiveRecord::Base

def self.sign_in_from_omniauth(auth)
    find_by(provider: auth['provider'], uid: auth['uid']) || create_user_from_omniauth(auth)
end

def self.create_user_from_omniauth(auth)
    create(
        provider: auth['provider'],
        uid: auth['uid'],
        name: auth['info']['name'],
        first_name: auth['info']['first_name'],
        last_name: auth['info']['last_name'],
        image: auth['info']['image'],
        email: auth['info']['email']
        )
end
end

我的控制器/sessions_controller.rb,

class SessionsController < ApplicationController

def create
    auth = request.env["omniauth.auth"]
    #session[:omniauth] = auth.except('extra')
    user = User.sign_in_from_omniauth(auth)
    session[:user_id] = user.id
    redirect_to root_url
end

def destroy
    session[:user_id] = nil
    session[:omniauth] = nil
    redirect_to root_url
end

end

对于我重新设置服务器的解决方案的每一次尝试,是否 rake db:drop db:create db:migrate。我检查了我的数据库并存储了名称、uid 和图像链接,但没有存储电子邮件......另外:在 Facebook 上,在“状态和评论”侧选项卡下,我对“你想要让这个应用程序及其所有实时功能对公众可用?”,启用电子邮件、public_profile 和 user_friends。

我也试过下载omniauth-facebook 2.0.1。不知道该怎么做,请帮忙:)。这似乎是人们可能遇到的常见问题......

4

0 回答 0