Search code examples
authenticationpaperclipomniauth

how to retrieve facebook, twitter, and google profile images without error using paperclip in rails 3.2


i have setup devise to handle my authentication and am also using omniauth to do the third party authentication which also works well for me. i can retrieve the name, nickname, by setting this method in my user model

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
      user.username = auth.info.first_name
      user.email = auth.info.email
      user.photo = auth.info.image

    end
  end

and i use paperclip to handle my image processing whenever i try to authenticate a user with

user.photo = auth.info.image

i get an error like this

Paperclip::AdapterRegistry::NoHandlerError in OmniauthCallbacksController#facebook

No handler found for "http://graph.facebook.com/100006033401739/picture?type=square"

anyway around this or is there something am not doing right?

my OmniauthCallbacksController controller is as thus:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :twitter, :all
  alias_method :facebook, :all
  alias_method :google_oauth2, :all
end

Solution

  • The problem is that paperclip requires an image file and you are just passing an image url.

    I'd suggest having 2 different attributes: photo and remote_photo:

    • If logging in with facebook, google and twitter, you would set the remote_photo attribute to the auth.info.image returned.

    • If uploading a photo, you would use the photo attribute.

    However, if you want to download a person's facebook, google and twitter picture anyway, you can do this:

    user.photo = URI.parse(auth.info.image) if auth.info.image?
    

    This feature is kinda new so be sure that the version of Paperclip you are using is > 3.1.3.