Skip to content

Instantly share code, notes, and snippets.

@kamloops
Created November 14, 2013 20:01
Show Gist options
  • Save kamloops/7473377 to your computer and use it in GitHub Desktop.
Save kamloops/7473377 to your computer and use it in GitHub Desktop.
LinkedIn OAuth2 flow with POST to /accessToken
def index
authorize_oauth2
end
def authorize_oauth2
# Redirect to /authorization endpoint
redirect_to client.auth_code.authorize_url(:state => 'thisisatest', :redirect_uri => REDIRECT_URI)
end
def accept
# Strip out auth code from session param
code = params[:code]
authorize_user(code)
end
def authorize_user(code)
# Build out URI
uri = URI('https://www.linkedin.com/uas/oauth2/accessToken')
linkedin_uri = URI.parse('https://www.linkedin.com/uas/oauth2/accessToken')
https = Net::HTTP.new(linkedin_uri.host, linkedin_uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
# make POST call to /accessToken endpoint
postData = https.request_post(linkedin_uri.path,'grant_type=authorization_code&code='+ code +'&redirect_uri=http%3A%2F%2F0.0.0.0%3A3000%2Faccept&client_id=75y05cochdr69u&client_secret=rZ1bUz8om3yaXFw2')
# Put data into hash and display in console - let's see the response header
puts postData.to_hash
body = JSON.parse(postData.body)
# Extract access token
token = body["access_token"]
response = https.request_get('https://api.linkedin.com/v1/people/~:(first-name)?oauth2_access_token='+ token)
puts response.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment