Search code examples
ruby-on-railsrubyruby-on-rails-3oauthlinkedin-api

Large profile image with omniauth linkedin Ruby gem


I'm using the omniauth-linkedin gem to allow users to log into my Rails application using their LinkedIn account. I'm currently using auth.info.image to store the user's LinkedIn profile image URL:

user.rb

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.email = auth.info.email
      user.linkedin_photo_url = auth.info.image
      user.password = Devise.friendly_token[0,20]
    end

However, the image is very small (50x50). Is there another method besides auth.info.image I could use in order to pull the large profile image found on the user's main profile page?

Thanks!

EDIT: I'm using the omniauth-linkedin and omniauth gems. It looks like the linkedin gem has a method with an option to determine image size but I'm struggling with implementing it with the omniauth-linkedin gem. This readme explains that it's possible but the explanation is lacking some details. Can someone help me figure this out?

https://github.com/skorks/omniauth-linkedin#using-it-with-the-linkedin-gem


Solution

  • One way to retrieve profile image in original size is by making separate API call.

    1. include gem 'linkedin'
    2. create initializer file /config/initializers/linkedin.rb with content:

      LinkedIn.configure do |config| config.token = "your LinkedIn app consumer_key" config.secret = "your consumer_secret" end

    3. in your self.from_omniauth method replace line

      user.linkedin_photo_url = auth.info.image

    with

    client = LinkedIn::Client.new
    client.authorize_from_access(auth.extra.access_token.token, auth.extra.access_token.secret)
    user.linkedin_photo_url = client.picture_urls.all.first
    

    DONE