Skip to content

Instantly share code, notes, and snippets.

@daviddefco
Last active August 15, 2021 01:33
Show Gist options
  • Save daviddefco/4557352 to your computer and use it in GitHub Desktop.
Save daviddefco/4557352 to your computer and use it in GitHub Desktop.
Code to get access to both basic and full linkedin profiles. You make one execution with the first code block to get token and token secret values and then can remove this code, assign values to variables and get the access_token directly
require 'oauth'
# api_key and api_secret values are got from the registration of your application in LinkedIn:
# http://developer.linkedin.com/
api_key = 'Your API Key'
api_secret = 'Your API Secret'
# access_token and token_secret are known after the first execution code block
access_token = 'The token we get below'
token_secret = 'The token secret we get below'
# The configuration will ask for permission to get access to both basic and full profile
configuration = { :site => 'https://api.linkedin.com',
:authorize_path => '/uas/oauth/authenticate',
:request_token_path => '/uas/oauth/requestToken?scope=r_basicprofile+r_fullprofile',
:access_token_path => '/uas/oauth/accessToken' }
consumer = OAuth::Consumer.new(api_key, api_secret, configuration)
# FIRST EXECUTION CODE: You just execute this code to find out the access_token and token_secret
# and then next executions just fill variables with these values and execute the next executions
# code block
request_token = consumer.get_request_token
# Now get the access URL and paste it to your browser
puts request_token.authorize_url
# After granting access get the PIN value that appears in the page and put in the variable to get
# the acess_token and retrieve the token and secret for next executions
pin = 'The PIN we get in the page after granting access to the app'
access_token = request_token.get_access_token(:oauth_verifier => pin)
# Now get access_token adn toekn_secret values and assign to above variables for further executions
puts "Access Token: #{access_token.token}"
puts "Access Token Secret: #{access_token.secret}"
# NEXT EXECUTIONS CODE: Access token can be instantiated without having to go to browser, just with
# variables we got in the first execution block
access_token = OAuth::AccessToken.new(consumer, access_token, token_secret)
# Get a field from the basic profile
puts access_token.get('https://api.linkedin.com/v1/people/~:(first-name)').body
# Get a field from the full profile
puts access_token.get('https://api.linkedin.com/v1/people/~:(publications)').body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment