2012-04-11 75 views
1

嗨,我正在使用linkedin gem與rails連接。這是我的控制器。如何在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和位置參數,但它不工作,如何獲得這些信息還教育currentcompanyname等

請幫我我google搜索,但無法獲得正確的信息。

+1

是否https://rubygems.org/gems/linkedin幫助嗎? – Reactormonk 2012-04-11 10:03:44

+0

我很驚訝地發現,沒有人會爲此得到答案! – SAR 2012-04-11 10:30:23

回答

1

使用「LinkedIn」的寶石,你可以做以下讓你正在尋找返回字符串數組的信息。

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

相關問題