我的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个人资料照片。
答案 0 :(得分:0)
auth.info.image_url
中可用。在
您的示例代码已使用auth.info.avatar