Aym*_*lah 7 facebook ruby-on-rails devise omniauth-facebook
我一直在努力设置我的Omniauth for facebook我不知道我做错了什么.
我无法收到用户的电子邮件.返回的哈希只包含"name"和"uid",甚至不包含"first_name"和"last_name"
devise.rb:
config.omniauth :facebook, "KEY", "SECRET"
Run Code Online (Sandbox Code Playgroud)
omniauth_callbacks_controller.rb:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
logger.info request.env["omniauth.auth"]
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
Run Code Online (Sandbox Code Playgroud)
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
has_many :authentications
def self.from_omniauth(auth)
logger.info auth
user = where(email: auth.info.email).first
if(user != nil)
user.authentications.where(provider: auth.provider, uid: auth.uid).first_or_create do |l|
user.authentications.create!(user_id: user.id,
provider: auth.provider,
uid: auth.uid)
end
else
user = User.create!(email: auth.info.email,
password: Devise.friendly_token[0,20],
first_name: auth.info.first_name,
last_name: auth.info.last_name)
user.authentications.create!(user_id: user.id,
provider: auth.provider,
uid: auth.uid)
end
user
end
end
Run Code Online (Sandbox Code Playgroud)
registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
end
end
Run Code Online (Sandbox Code Playgroud)
routes.rb中:
devise_for :users, :controllers => { registrations: 'registrations', omniauth_callbacks: 'omniauth_callbacks' }
Run Code Online (Sandbox Code Playgroud)
返回哈希:
#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1444504014 token="TOKEN">
extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash id="1506781179612589" name="Ayman Salah">> info=#
<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/1506781179612589/picture" name="Ayman Salah"> provider="facebook" uid="1506781179612589">
Run Code Online (Sandbox Code Playgroud)
Sia*_*Sia 10
确保你没有在信息字段请求中放置空格(另一个答案可能是错误的).
您需要特别要求从信息字段哈希返回电子邮件,因为它不是默认情况下.
如果您在没有Devise的情况下使用Omniauth-Facebook,请在config/initializers/omniauth.rb文件中进行设置,如下所示:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, Rails.application.secrets.omniauth_provider_key, Rails.application.secrets.omniauth_provider_secret,
:scope => 'email', :display => 'popup', :info_fields => 'name,email'
end
Run Code Online (Sandbox Code Playgroud)
这个信息有点隐藏在omniauth-facebook gem的GitHub自述文件的配置部分的最后.
但是,使用Devise plus Omniauth,你可以在config/initializers/devise.rb中设置它(如果字符串中没有空格!):
config.omniauth :facebook, 'app_id', 'app_secret', scope: 'email', info_fields: 'name,email'
Run Code Online (Sandbox Code Playgroud)
我只需要在devise.rb中添加它来检索电子邮件:
config.omniauth :facebook, "KEY", "SECRET", scope: 'email', info_fields: 'email, name'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4923 次 |
| 最近记录: |