Вопрос

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

Это было полезно?

Решение 2

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

Другие советы

I know it's been awhile, but I was just looking for this and thought I'd leave it here. The solution is fine, but will cause an extra call. Omniauth is already doing a fetch of the profile so we just have to tell it to also get the original picture

linkedin_options = {
  scope: 'r_fullprofile r_emailaddress',
  fields: ['id', 'email-address', 'first-name', 'last-name', 'headline', 'location', 'industry', 'picture-url', 'public-profile-url', "picture-urls::(original)"]
}
provider :linkedin, app_id,app_secret, linkedin_options

pictureUrls will be available in the extra info.

To get the image, use auth_hash[:extra][:raw_info][:pictureUrls][:values].first

image = auth.extra.raw_info.pictureUrls.values.last.first

This is using a combination of the omniauth gem, devise, and paperclip that works for me:

config/initializers/devise.rb

config.omniauth :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'],
          scope: 'r_basicprofile r_emailaddress',
          fields: ['id', 'email-address', 'first-name', 'last-name',   'picture-urls::(original)']

app/models/user.rb

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create.tap do |user| # .tap will run the |user| block regardless if is first or create
    user.email = auth.info.email
    user.password = Devise.friendly_token[0,20]
    user.firstname = auth.info.first_name
    user.lastname = auth.info.last_name

    if auth.provider == 'facebook'
      user.avatar = URI.parse(auth.info.image)
    elsif auth.provider == 'linkedin'
      user.avatar = URI.parse(auth.extra.raw_info.pictureUrls.values.last.first)
    end

    user.skip_confirmation!
  end
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top