0

LinkedIn's API lets a user submit their email and password into a form that looks like the following:

https://www.linkedin.com/uas/oauth2/authorization?client_id=nhrepd2sjd6k&raise_errors=true&redirect_uri=https%3A%2F%2Fwww.treatings.co%2Fv1%2Ftoken_exchanges&response_type=code&scope=r_fullprofile+r_emailaddress&state=JV4qrQfA8p%2BAKtyq1DFgaaXeRZSg50lQrAU%2Fc%2BBSXaU%3D

With the correct credentials, LinkedIn redirects to the redirect URI in the above URL with code and state parameters:

https://www.treatings.co/v1/token_exchanges?code=CODE&state=STATE

I have two questions:

  1. How can I use Ruby to submit the form's URL with an email and password?
  2. Once I get a response, how do I obtain the URL in the second?

Thanks!

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
po.studio
  • 4,007
  • 5
  • 25
  • 37

1 Answers1

0

Ruby comes with Net::HTTP and OpenURI. Of the two, OpenURI is the easier to use.

Other gems exist to do the same things, but to start I'd recommend sticking with OpenURI until you find specific reasons to look elsewhere.

OpenURI's documentation has plenty of examples to get you started, but the basic command is:

open('http://www.example.com/index.html').read

which would return the contents of the page as a string. You can replace the URL above with your URL containing the form contents, which will cause the server to respond with whatever it would normally return.

Because you're using a HTTPS URL, OpenURI will sense that and use a SSL connection automatically. That is also covered in the documentation.

Redirection will be handled automatically by OpenURI, and you can ask it what its landing URL was. At that point you can use Ruby's URI class to parse the URL, extract the query using query, then cleanly retrieve the values using decode_www_form.

There are plenty of examples here and around the internet to point you in the right direction.


I'm not sure that OpenURI supports POST....

Then there are plenty of gems that do support it, or drop down a level and use Net::HTTP. A search for "ruby http clients" reveals much.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • it's not reading the page that is giving me difficulty - it's submitting the form and grabbing the redirect url. would appreciate any ideas you have – po.studio Jan 30 '15 at 21:27
  • I'd suggest you read the documentation and poke at OpenURI in IRB. It's not hard to figure this out. – the Tin Man Jan 30 '15 at 21:30
  • I'm not sure that OpenURI supports POST. http://stackoverflow.com/questions/242602/how-do-i-make-a-post-request-with-open-uri – po.studio Jan 30 '15 at 21:49
  • Where in your question did you specify that requirement? The URL you show is usually a GET request, since the parameters are in the URL. – the Tin Man Jan 30 '15 at 22:05