与omniauth linkedin Ruby宝石的大型配置文件图像

时间:2014-03-22 02:56:25

标签: ruby-on-rails ruby ruby-on-rails-3 oauth linkedin

我正在使用omniauth-linkedin gem允许用户使用他们的LinkedIn帐户登录我的Rails应用程序。我目前正在使用auth.info.image来存储用户的LinkedIn个人资料图片网址:

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

然而,图像非常小(50x50)。除了auth.info.image之外还有其他方法可以用来拉取用户主要个人资料页面上的大型个人资料图片吗?

谢谢!

编辑:我正在使用omniauth-linkedinomniauth宝石。看起来linkedin gem有一个方法可以选择确定图像大小,但我正在努力用omniauth-linkedin gem实现它。本自述文件解释说这是可能的,但解释缺乏一些细节。有人可以帮我解决这个问题吗?

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

4 个答案:

答案 0 :(得分:7)

我知道它已经有一段时间了,但我只是在寻找这个并且以为我会把它留在这里。解决方案很好,但会引起额外的通话。 Omniauth已经在对配置文件进行提取,因此我们只需告诉它也可以获取原始图片

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将在额外信息中提供。

要获取图像,请使用auth_hash[:extra][:raw_info][:pictureUrls][:values].first

答案 1 :(得分:6)

检索原始大小的个人资料图片的一种方法是进行单独的API调用。

  1. 包括gem' linkedin'
  2. 使用内容创建初始化文件/config/initializers/linkedin.rb:

    LinkedIn.configure do | config |   config.token ="你的LinkedIn app consumer_key"   config.secret ="你的consumer_secret" 端

  3. 你的self.from_omniauth方法中的
  4. 替换行

    user.linkedin_photo_url = auth.info.image

  5. 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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这是使用omniauth gem,devise和paperclip的组合,对我有用:

配置/初始化/ 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)']

应用程序/模型/ 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