拉数据&解析LinkedIn数据

时间:2014-02-20 07:09:09

标签: ruby-on-rails devise linkedin

我在我的新rails 4应用程序中使用gem'omniauth-linkedin-oauth2'和gem'depaise'以便能够验证用户身份。我能够做到这一点,现在我想获取他们接受时获得的信息,并能够将其中的某些部分添加到我的数据库中。我已经读过,我得到的信息是JSON哈希。我希望能够获取信息的某些部分,然后能够在视图中显示它。这是我的config / initalizers / devise.rb文件,我要求LinkedIn acct进行某些访问。

require 'omniauth-linkedin-oauth2' 
config.omniauth :linkedin, "APP_ID", "APP_Secret", :scope => 'r_fullprofile                      r_emailaddress r_network', p  :fields => ["id", "email-address", "first-name", "last-name",  "headline", "industry", "picture-url", "public-profile-url", "location", "connections"]

抱歉'r_fullprofile和r_emailaddresses之间存在奇怪的差距。

这是我的application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Danapp</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>

</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

<% if user_signed_in? %>
Signed in as <%= current_user.name %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Linkedin",user_omniauth_authorize_path(:linkedin) %>
<% end %>

<%= yield %>

</body>


</html>

我正在寻找如何使用我的rails应用程序启动LinkedIn集成的方法。任何帮助将不胜感激,再次感谢阅读本文。

1 个答案:

答案 0 :(得分:0)

我喜欢在用户名为Identity之前添加额外的图层来保存从omniauth策略中获取的信息。有了这个,你就可以为同一个用户添加额外的策略(Twitter,Github,Google ......)。

那么它是如何工作的:策略的(linkedin)回调将创建一个标识并使用API​​响应中的数据加载它。 然后,您可以根据此标识查找或创建新用户。在此步骤中,您将身份信息加载到用户信息中。

我的代码来自:https://github.com/alex-klepa/rails4-bootstrap-devise-cancan-omniauth 我会在找到这个代码时添加原始来源。但这是我的实现:

在我的controllers / users / omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
    identity = Identity.from_omniauth(request.env["omniauth.auth"])
    user = identity.find_or_create_user(current_user)

  # if user.save
    if user.valid?
      logger.debug "User signed in:"
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      logger.debug "User not valid"
      logger.debug "#{user.errors.inspect}"
      user.errors.full_messages.each do |msg|
       flash.notice = msg
      end
      flash[:error] = 'User/Company not allowed.'
      # sign_in user
      redirect_to root_url
    end
  end

  alias_method :linkedin, :all
  # alias_method :twitter, :all
end

在我的models / identity.rb中:

class Identity < ActiveRecord::Base
  belongs_to :user
  serialize :company
  def self.from_omniauth(auth)
    identity = where(auth.slice(:provider, :uid)).first_or_create do |identity|
    identity.provider     = auth.provider
    identity.uid          = auth.uid
    identity.token        = auth.credentials.token
    identity.secret       = auth.credentials.secret if auth.credentials.secret
    identity.expires_at   = auth.credentials.expires_at if auth.credentials.expires_at
    identity.email        = auth.info.email if auth.info.email
    identity.image        = auth.info.image if auth.info.image
    identity.nickname     = auth.info.nickname
    identity.first_name   = auth.info.first_name
    identity.last_name    = auth.info.last_name
    # I need to refactor the line below urgently
    identity.company      = auth.extra.raw_info.threeCurrentPositions.values.map { |n| n.first.company.name if n.kind_of?(Array) }.compact
  end
  identity.save!
    if !identity.persisted?
      redirect_to root_url, alert: "Something went wrong, please try again."
    end
    identity
 end
 def find_or_create_user(current_user)
   if current_user && self.user == current_user
   logger.debug "User logged in and the identity is associated with the current user"
   # User logged in and the identity is associated with the current user
   return self.user
  elsif current_user && self.user != current_user
   logger.debug "User logged in and the identity is not associated with the current user"
   logger.debug "so lets associate the identity and update missing info"
   # User logged in and the identity is not associated with the current user
   # so lets associate the identity and update missing info
   self.user = current_user
   self.user.email       ||= self.email
   self.user.image       ||= self.image
   self.user.first_name  ||= self.first_name
   self.user.last_name   ||= self.last_name
   self.user.company     ||= self.company
   # self.user.skip_reconfirmation!
   self.user.save!
   self.save!
   return self.user
 elsif self.user.present?
   logger.debug "User not logged in and we found the identity associated with user"
   logger.debug "so let's just log them in here"
   # logger.debug "#{self.inspect}"
   # User not logged in and we found the identity associated with user
   # so let's just log them in here
   return self.user
 else
   logger.debug "No user associated with the identity so we need to create a new one"
   # logger.debug "#{self.inspect}"
   # No user associated with the identity so we need to create a new one
   user = User.where(email: self.email).first
   if user.nil?
     self.build_user(
       email: self.email,
       image: self.image,
       first_name: self.first_name,
       last_name: self.last_name,
       company: self.company
     )
     # self.user.save!(validate: false)
     self.save!
     self.user.save
   else
     self.user_id = user.id
     self.save!
     self.user.save
   end
   return self.user
 end
end
end