![](/img/trans.png)
[英]Rails: retrieving image from Facebook after Omniauth login with Devise
[英]Rails app is not taking facebook image after fb login
我的rails应用程序具有fb登录,当通过fb登录对用户进行身份验证时,我会收到他的电子邮件和全名,但无法获取他的个人资料图片。
我的应用程序助手
module ApplicationHelper
def avatar_url(user)
if user.avatar
user.avatar
else
"/images/missing.png"
end
end
结束
Omniauth_callbacks_controller.rb是,
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
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
def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
else
redirect_to root_path, flash: { error: 'Authentication failed!' }
end
结束
结束
我的用户模型是
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:google_oauth2, :facebook]
validates :fullname, presence: true, length: {maximum: 40}
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.fullname = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.avatar = auth.info.avatar
user.password = Devise.friendly_token[0,20]
end
end
end
我从Facebook获得授权,但未获得fb个人资料图片。
auth.info.image_url
可用。 在示例代码中,您使用了auth.info.avatar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.