Question

LinkedIn API has two parts: 1) get authorization code from LinkedIn (GET request) 2) use authorization code with 10 seconds or so to get access token from LinkedIn (POST request). My example requests are below:

Part One below, I can do successfully in the browser, get redirect to LinkedIn, and get the authorization code. https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=XXXXX&state=ddlkjdfkljdfkljdfk&redirect_uri=http%3A//localdev.boxuno.com%3A8888/auth/linkedinOauth1Callback

Part Two, I can do successfully using curl as below. curl -v --data "grant_type=authorization_code&code=XXXXX&redirect_uri=http%3A//localdev.boxuno.com%3A8888/auth/linkedinOauth1Callback&client_id=XXXXX&client_secret=XXXXX" https://www.linkedin.com/uas/oauth2/accessToken

BUT if I use urllib in Python to construct the http POST request. It doesn't work: I get a 400. response = urllib2.urlopen(urllib2.Request(url, urllib.urlencode({'code': 'XXXXX', 'grant_type':'authorization_code', 'redirect_uri': 'http%3A//localdev.boxuno.com%3A8888/auth/linkedinOauth1Callback', 'client_id': 'XXXXX', 'client_secret': 'XXXXX})))

I've made sure the redirect URI in steps 1 & 2 match, that I call the second step quickly (so the authorization code doesn't expire), and made sure the client_id's match in both calls. I don't get why the Python version always fails but the curl version works. Also tried explicitly adding the header below b/c the curl request has it, but no dice. "Content-Type" : "Type: application/x-www-form-urlencoded"

Help!

Was it helpful?

Solution

I had done something dumb. I had passed the arguments both the body and as parameters on the end of the URL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top