Oauth 2.0 Ruby wrapper for the LinkedIn API. Heavily inspired by pengwynn/linkedin's Oauth 1.0 Linkedin API interface. Most methods are the same as pengwynn/linkedin's gem, but major terminology changes make this incompatible.
Installation
[sudo] gem install linkedin-oauth2
Usage
Authenticate Overview
LinkedIn's API uses OAuth 2.0 for authentication. Luckily, this gem hides most of the gory details from you.
The gory details can be found here
For legacy support of LinkedIn's OAuth 1.0a api, refer to the pengwynn/linkedin gem.
Basically, you need 3 things to start using LinkedIn's API:
- Your application's
client_idaka API Key - Your application's
client_secretaka Secret Key - An
access_tokenfrom a user who authorized your app.
If you already have a user's access_token
Then you have already authenticated! Encoded within that access token are all of the permissions you have on a given user. Assuming you requested the appropriate permissions, you can read their profile, grab connections, etc.
client = LinkedIn::Client.new("<your client_id>",
"<your client_secret>",
"<user access_token>")
client.profile
You may also use the set_access_token method.
client.set_access_token("<user access_token>", )
If you need to fetch an access_token for a user.
There are 4 steps:
Setup a client using your application's
client_idandclient_secretclient = LinkedIn::Client.new('your_client_id', 'your_client_secret')
Get the
authorize_urlto bring up the page that will ask a user to verify permissions & grant you access.authorize_url = client.authorize_url
Once a user signs in to your OAuth 2.0 box, you will get an
auth_codeaka code. (Check in url you were directed to after a successful auth). Use this auth code to request the access token.access_token = client.request_access_token("
") Once you have an
access_token, you can request profile information or anything else you have permissions for.client.profile
Profile examples
# 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')
More examples in the examples folder.
For a nice example on using this in a Rails App.
If you want to play with the LinkedIn api without using the gem, have a look at the apigee LinkedIn console.
Migration from OAuth 1.0a to OAuth 2.0
Overall changes
- The term
consumeris now referred to as theclient - The terms
token,consumer token, orconsumer keyin OAuth 1.0 are now referred to asclient_idin OAuth 2.0 - The terms
secret, orconsumer secretin OAuth 1.0 are now referred to asclient_secretin OAuth 2.0 - In OAuth 1.0 there is both an
auth tokenand anauth secret. OAuth 2.0 combines these into a singleaccess token. - The terms
auth token,auth key,auth secret,access secret,access token, oraccess keyhave all been collapsed and are now referred to as theaccess token. requirelinkedin-oauth2instead oflinkedin- Removed proxy options
Gem api changes
- In general, any place that said "consumer" now says "client"
- The
authorize_from_requestmethod has been deprecated. Instead navigate to the url fromauthorize_urlthen enter the returned code into therequest_access_tokenmethod. - The
authorize_from_accessmethod has been deprecated. Instead initialize theLinkedIn::Client.newwith theaccess_token.client = Linkedin::Client.new(client_id, client_secret, access_token)
Note on Patches/Pull Requests
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
Testing
Run bundle install
Copyright
Copyright (c) 2013 Evan Morikawa. See LICENSE for details.
