29 November 2009
Filed under Blog

Link LinkedIn into your next Ruby application

LinkedIn launched its new API last week, allowing any developer brave enough to implement OAuth to integrate with one of the largest social networks around. Seeking an opportunity to really understand OAuth more fully and play around with the LinkedIn API at the same time, I created the LinkedIn Ruby Gem.

What's exposed?

The LinkedIn API documentation could use some organization. To save you some digging, here are the main aspects of the REST API.

Profile
View public and full information for LinkedIn members based on the authenticated user, member ID, or public profile URL. Visibility of profile information depends on the distance the target user is to the authenticated user in his LinkedIn network.
Connections
Retrieve the authenticated user's social graph
Status
Let everyone know what you're up to, or see what they're working on.
Search
Find LinkedIn public profiles based on several criteria.
Invitations
Invite users to connect on behalf of the authenticated user.

Getting started

Before you can use the API, you'll need to set up your API keys.

Getting Started with the LinkedIn API

Ruby gem installation

sudo gem install linkedin

Usage

Start by authenticating via OAuth. To play around from irb, you'll need to authenticate via the PIN method.

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')
rtoken = client.request_token.token
rsecret = client.request_token.secret

# to test from your desktop, open the following url in your browser
# and record the pin it gives you
client.request_token.authorize_url
=> "https://api.linkedin.com/uas/oauth/authorize?oauth_token=<generated_token>"

# then fetch your access keys
client.authorize_from_request(rtoken, rsecret, pin)
=> ["OU812", "8675309"] # <= save these for future requests

# or authorize from previously fetched access keys
client.authorize_from_access("OU812", "8675309")

# you're now free to move about the cabin, call any API method

Be sure and save those access keys and just call authorize_from_access in subsequent irb sessions.

Grabbing a profile

Once authenticated via OAuth, you can grab the profile for the authenticated user simply by calling profile

# get the profile for the authenticated user
client.profile

You can also grab the profile for another user via the member id or public profile url:

# 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')

If you want to see more than first-name,last-name, and headline, you'll need to pass in a list of fields to return:

# get a profile for someone found in network via ID
client.profile(:id => 'gNma67_AdI', :fields => %w(first-name, last-name, headline, positions, education))

For the full list of possible fields, check the docs.

More examples

For examples using the other API methods, check out the examples folder in the source on GitHub.

Summary

I want to give a big hat tip to John Nunemaker for helping me implement OAuth in this gem and for inspiring me with his ultra-popular Twitter gem.

Once you navigate the documentation, I believe you'll find the LinkedIn API to be a powerful platform to integrate into your Ruby applications. Install the gem and go build somethting cool.

Like this? Why not grab the feed?