Skip to content

Instantly share code, notes, and snippets.

@frausto
Last active January 26, 2017 17:02
Show Gist options
  • Save frausto/7939449 to your computer and use it in GitHub Desktop.
Save frausto/7939449 to your computer and use it in GitHub Desktop.
Linkedin Oauth: Exchanging JSAPI token for a REST token using Ruby/Rails and the linkedin gem
$.getScript("https://platform.linkedin.com/in.js?async=true", ->
IN.init({
api_key: <<API Key>>,
authorize: true,
credentials_cookie: true
})
)
linkedinImport = ->
IN.User.authorize( ->
$.post('https://localhost:3000/oauth/linkedin')
)
#initializer
ENV["LINKEDIN_APP_ID"] = #<<OAuth User Token>>
ENV['LINKEDIN_SECRET'] = #<<OAuth User Secret>>
ENV['LINKEDIN_API_KEY'] = #<<API Key>>
ENV['LINKEDIN_API_SECRET'] = #<<Secret Key>>
class OauthController < Api::ApiController
def linkedin
linkedin_auth = JSON.parse(request.cookies["linkedin_oauth_#{ENV['LINKEDIN_API_KEY']}"])
client = LinkedIn::Client.new(ENV["LINKEDIN_API_KEY"], ENV['LINKEDIN_API_SECRET'])
consumer = client.consumer
token = consumer.get_access_token(nil, {}, {
'xoauth_oauth2_access_token' => linkedin_auth['access_token']
})
client.authorize_from_access(token.token, token.secret)
profile_info = client.profile({
:fields => ['id', 'first-name', 'last-name', 'picture-url', 'positions:(title,company:(name))']
})
#do whatever
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment