如何使用linkedin gem和omniauth获得更精细的LinkedIn用户信息?

时间:2014-01-21 22:45:39

标签: ruby-on-rails api devise linkedin omniauth

我想从他们的LinkedIn个人资料(公司名称,职位等)中获取用户的位置信息,但我得不到基本个人资料信息(身份证,姓名,电子邮件,标题和图片)。我猜这只是一种语法,因为我在用户身份验证时请求访问所有字段。

来自user.rb的

    def self.from_omniauth(auth)
      where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.email = auth.info.email
        user.headline = auth.info.headline
        **user.company = auth.info.company**
        user.avatar = auth.info.image
        user.password = SecureRandom.urlsafe_base64(n=6)
        user.save!    
    end  
来自devise.rb

  config.omniauth :linkedin, "abcdefghijk", "abcdefghijk", 
  # :scope => 'r_basicprofile r_emailaddress rw_nus r_fullprofile r_contactinfo r_network rw_company_admin',
  # :fields =>
  :scope => 'r_basicprofile r_emailaddress rw_nus r_fullprofile r_contactinfo r_network rw_company_admin', 
  :fields => ["id", "email-address", "first-name", "last-name", 
                  "headline", "industry", "picture-url", "public-profile-url", 
                  "location", "connections", "skills", "date-of-birth", "phone-numbers",        
                  "educations", "three-current-positions" ]

我是否需要向devise.rb添加字段,还是我是白痴并格式化user.company = auth.info.company错误?

谢谢!

2 个答案:

答案 0 :(得分:0)

[编辑] 好吧,找到了。 devise.rb字段定义错误。 (可能已经过时了,我使用了同样的东西,它是从某处复制的。)

现在正确的名字只是"位置"而不是"三个当前位置"

之后,auth.extra.info.positions变得可用(使用这些参数:https://developer.linkedin.com/docs/fields/positions

答案 1 :(得分:-1)

访问用户信息ID的格式为:

user.name = auth["info"]["name"]