【问题标题】:Large profile image with omniauth linkedin Ruby gem带有omniauth linkedin Ruby gem的大型个人资料图片
【发布时间】:2014-04-29 15:40:35
【问题描述】:

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

用户.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 gems。看起来linkedin gem 有一个方法可以选择确定图像大小,但我正在努力使用omniauth-linkedin gem 来实现它。本自述文件解释了这是可能的,但解释缺少一些细节。有人可以帮我解决这个问题吗?

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

【问题讨论】:

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


    【解决方案1】:

    我知道这已经有一段时间了,但我只是在寻找这个并想我会把它留在这里。解决方案很好,但会导致额外的呼叫。 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

    【讨论】:

    • 甜蜜!我仍然不明白为什么linkedin 一直在做与其他服务不同的事情并且不尊重最佳实践......
    【解决方案2】:

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

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

      LinkedIn.configure 做 |config| config.token = "您的 LinkedIn 应用程序 consumer_key" config.secret = "你的消费者秘密" 结束

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

      user.linkedin_photo_url = auth.info.image

    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
    

    完成

    【讨论】:

    • 天哪,几个月来我一直在努力做到这一点!非常感谢!
    • 不客气@RizalYusoff :) 祝你的项目好运。
    【解决方案3】:
    image = auth.extra.raw_info.pictureUrls.values.last.first
    

    【讨论】:

      【解决方案4】:

      这是使用适合我的omniauth gem、设计和回形针的组合:

      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
      

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        相关资源
        最近更新 更多