1

こんにちは私はLinkedIngemを使用してRailsからLinkedInに接続しています。これが私のコントローラーです。

class LoginsController < ApplicationController
  helper_method :current_user

  def show
  end

  def create
    request_token = consumer.get_request_token(:oauth_callback => callback_login_url)
    Rails.cache.write(request_token.token, request_token.secret)
    redirect_to request_token.authorize_url
  end

  def callback
    request_token = OAuth::RequestToken.new(consumer, params[:oauth_token], Rails.cache.read(params[:oauth_token]))
    access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
    session[:access_token] = access_token.token
    session[:access_token_secret] = access_token.secret
    redirect_to :action => :show
  end

  def logout
    reset_session
    redirect_to :action => :show
  end

  private
  CONSUMER_KEY = {
    :key => "ddddd",
    :secret => "eeeeeeeeee"
  }
  CONSUMER_OPTIONS = { :site => 'https://api.linkedin.com',
                     :authorize_path => '/uas/oauth/authorize',
                     :request_token_path => '/uas/oauth/requestToken',
                     :access_token_path => '/uas/oauth/accessToken' }

  def consumer
    @consumer ||= OAuth::Consumer.new( CONSUMER_KEY[:key], CONSUMER_KEY[:secret], CONSUMER_OPTIONS)
  end

  def access_token
    if session[:access_token]
      @access_token ||= OAuth::AccessToken.new(consumer, session[:access_token], session[:access_token_secret])
    end
  end

  def current_user
    if access_token
      @current_user ||= JSON.parse(access_token.get('http://api.linkedin.com/v1/people/~', 'x-li-format' => 'json').body)
    end
    @current_user
  end

end

これが私の見解です:

<% if current_user %>
  Welcome to Lovelinked , <%=link_to current_user['firstName'], current_user['siteStandardProfileRequest']['url'] %>
  <%=link_to current_user['lastName'], current_user['siteStandardProfileRequest']['url'] %>
  <%=button_to "Sign Out", logout_login_path, :method => :get %>
<% else %>
  Sign in with LinkedIn
  <%=button_to "Sign In", login_path, :method => :post %>
<% end %>

firstNameとlastNameを取得し、images_urlやlocationなどのパラメーターを渡すことはできますが、機能していません。これらの情報を取得する方法は、現在の会社名なども教育しています。

私がグーグルで検索するのを手伝ってください、しかし正しい情報を得ることができませんでした。

4

2 に答える 2

3

gemhttps://github.com/yatishmehta27/linkedin-scraperを試してlinkedin-scraper ください

于 2012-04-14T11:32:11.900 に答える
1

'linkedin' gemを使用すると、次のようにして、探している情報を文字列の配列として返すことができます。

user = client.profile(:fields => %w(positions))
companies = user.positions.all.map{|t| t.company}
# And if you want the most recent company it can be accessed via companies[0]

この情報はすべてhttps://github.com/pengwynn/linkedin/blob/master/examples/profile.rbにあります。

于 2012-07-30T22:42:44.263 に答える