### Accessing User Network Updates and Connections - Ruby Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet provides examples for accessing the authenticated user's network data. It shows how to retrieve network updates (optionally filtered by type), view connections, and get profile picture URLs for connections (including secure HTTPS options). ```Ruby # AUTHENTICATE FIRST found in examples/authenticate.rb # client is a LinkedIn::Client # get network updates for the authenticated user client.network_updates # get profile picture changes client.network_updates(:type => 'PICT') # view connections for the currently authenticated user client.connections # get the original picture-url for one of the connections client.picture_urls(:id => 'id_of_connection') # get the image over https instead of http client.picture_urls(:id => 'id_of_connection', :secure => "true") ``` -------------------------------- ### Complete Sinatra LinkedIn Authentication App Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet contains the full source code for a Sinatra application that integrates with LinkedIn for user authentication. It defines helper methods to check login status and retrieve user data, configures API keys, sets up session management, and handles the OAuth 1.0a flow through various routes (`/`, `/auth`, `/auth/logout`, `/auth/callback`). It also includes an embedded HAML template for the main page. ```Ruby require "rubygems" require "haml" require "sinatra" require "linkedin" enable :sessions helpers do def login? !session[:atoken].nil? end def profile linkedin_client.profile unless session[:atoken].nil? end def connections linkedin_client.connections unless session[:atoken].nil? end private def linkedin_client client = LinkedIn::Client.new(settings.api, settings.secret) client.authorize_from_access(session[:atoken]) client end end configure do # get your api keys at https://www.linkedin.com/secure/developer set :api, "your_api_key" set :secret, "your_secret" end get "/" do haml :index end get "/auth" do client = LinkedIn::Client.new(settings.api, settings.secret) request_token = client.request_token(:oauth_callback => "http://#{request.host}:#{request.port}/auth/callback") session[:rtoken] = request_token.token session[:rsecret] = request_token.secret redirect client.request_token.authorize_url end get "/auth/logout" do session[:atoken] = nil redirect "/" end get "/auth/callback" do client = LinkedIn::Client.new(settings.api, settings.secret) if session[:atoken].nil? pin = params[:oauth_verifier] atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin) session[:atoken] = atoken session[:asecret] = asecret end redirect "/" end __END__ @@index -if login? %p Welcome #{profile.first_name}! %a{:href => "/auth/logout"} Logout %p= profile.headline %br %div= "You have #{connections.total} connections!" -connections.all.each do |c| %div= "#{c.first_name} #{c.last_name} - #{c.headline}" -else %a{:href => "/auth"} Login using LinkedIn ``` -------------------------------- ### Accessing User Profiles - Ruby Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet shows various ways to retrieve user profile data using the linkedin gem. It includes fetching the authenticated user's profile, fetching profiles by ID or public URL, requesting specific fields, and performing a multi-email search. ```Ruby # AUTHENTICATE FIRST found in examples/authenticate.rb # client is a LinkedIn::Client # get the profile for the authenticated user client.profile # get a profile for someone found in network via ID client.profile(:id => 'gNma67_AdI') # get a profile for someone via their public profile url client.profile(:url => 'http://www.linkedin.com/in/netherland') # provides the ability to access authenticated user's company field in the profile user = client.profile(:fields => %w(positions)) companies = user.positions.all.map{|t| t.company} # Example: most recent company can be accessed via companies[0] # Example of a multi-email search against the special email search API account_exists = client.profile(:email => 'email=yy@zz.com,email=xx@yy.com', :fields => ['id']) ``` -------------------------------- ### Sending Messages to Connections - Ruby Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet demonstrates how to send a message to one or more recipients in the user's network using the linkedin gem. It requires prior authentication and the 'w_messages' permission. ```Ruby # AUTHENTICATE FIRST found in examples/authenticate.md # client is a LinkedIn::Client # send a message to a person in your network. you will need to authenticate the # user and ask for the "w_messages" permission. response = client.send_message("subject", "body", ["person_1_id", "person_2_id"]) ``` -------------------------------- ### Authenticating with LinkedIn API (OAuth2) - Ruby Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet demonstrates the OAuth2 authentication flow using the linkedin gem. It covers requiring the gem, initializing the client with API keys, generating the authorization URL with scopes, authorizing from the callback code, and authorizing from a previously saved access token. ```Ruby require 'rubygems' require 'linkedin' # get your api keys at https://www.linkedin.com/secure/developer client = LinkedIn::Client.new('your_consumer_key', 'your_consumer_secret') # If you want to use one of the scopes from linkedin you have to pass it in at this point # You can learn more about it here: http://developer.linkedin.com/documents/authentication # to test from your desktop, open the following url in your browser # and record the pin it gives you client.authorize_url(:redirect_uri => 'https:://www.yourdomain.com/callback', :state => SecureRandom.uuid, :scope => "r_basicprofile+r_emailaddress") #=> "https://api.linkedin.com/uas/oauth2/authorization?" # then fetch your access keys client.authorize_from_request(params[:code], :redirect_uri => 'https:://www.yourdomain.com/callback') #=> "OU812" # <= save this for future requests # NOTE: params[:code] is returned in the callback # or authorize from previously fetched access keys client.authorize_from_access("OU812") # you're now free to move about the cabin, call any API method ``` -------------------------------- ### Updating User Status/Sharing - Ruby Source: https://github.com/hexgnu/linkedin/blob/master/EXAMPLES.md This snippet demonstrates how to update the authenticated user's status or share content on their behalf using the linkedin gem's add_share method. It requires prior authentication. ```Ruby # AUTHENTICATE FIRST found in examples/authenticate.rb # client is a LinkedIn::Client # update status for the authenticated user client.add_share(:comment => 'is playing with the LinkedIn Ruby gem') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.