【问题标题】:Pulling data & Parsing LinkedIn data提取数据和解析 LinkedIn 数据
【发布时间】:2014-02-21 01:28:27
【问题描述】:

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

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>

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

【问题讨论】:

    标签: ruby-on-rails devise linkedin


    【解决方案1】:

    我喜欢在名为 Identity 的用户之前添加一个额外的层来保存您从omniauth 策略获得的信息的想法。这样,您就可以为同一用户添加额外的登录策略(Twitter、Github、Google...)。

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

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

    在我的控制器/用户/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
    

    在我的模型/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
    

    【讨论】:

    • Beto,在您的模型/身份.rb 中,如何访问您在介绍中所说的其他类型的社交媒体数据?
    • 好吧,在我的示例中,我没有访问任何其他社交媒体。您可以取消注释 alias_method :twitter, :all 并将 Twitter 策略添加到您的omniauth 并且用户将能够使用两者登录。由于创建此代码以接受各种策略,from_omniauth 方法仅查找所有策略共有的信息。正如我在identity.company = auth.extra.raw_info.threeCurrentPositions.values.map { |n| n.first.company.name if n.kind_of?(Array) }.compact 中所做的那样,您可以做的是加载linkedin 提供给我的公司。你可以做类似的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多