在fb登录后,Rails应用程序没有拍摄脸书图片

时间:2016-05-13 14:03:02

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip facebook-authentication

我的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个人资料照片。

1 个答案:

答案 0 :(得分:0)

  1. 看起来您正在使用 omniauth-facebook rails gem。在Facebook auth hash,图片网址在auth.info.image_url中可用。在 您的示例代码已使用auth.info.avatar
  2. 您似乎正在使用 Paperclip gem来存储图像。您 可以查看以下paperclip wiki下载图像 用于远程网址。 https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL