5
votes

I'm trying to access LinkedIn data via API (I don't have an app, I just want to access company data - or see what can be accessed). There are other questions here on this topic, but most are out of date (using packagaes which precede LinkedIn's current authorisation process).

I followed the LinkedIn documentation on authorisation: https://developer.linkedin.com/docs/oauth2

I created an application (using a nonsense website url as I do not have a website). This gave me a Client ID and Client Secret.

Using (out of date) stuff from LinkedIn (https://github.com/linkedin/api-get-started/blob/master/python/tutorial.py) I wrote:

import oauth2 as oauth
import urllib.parse as urlparse

consumer_key    =   'my client id e.g. sjd6ffdf6262d'
consumer_secret =   'my customer secret e.g. d77373hhfh'

request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
access_token_url =  'https://api.linkedin.com/uas/oauth/accessToken'
authorize_url =     'https://api.linkedin.com/uas/oauth/authorize'

consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)

resp,content = client.request(request_token_url, "POST")

request_token = dict(urlparse.parse_qsl(content))

clean_request_token = {}
for key in request_token.keys():
    clean_request_token[key.decode('ascii')] = request_token[key].decode('ascii')
request_token = clean_request_token

print ("Go to the following link in your browser:")
print ("%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']

This link takes me to a website where I 'give permission', and am then shown a pin code. Using this pin (called oauth_verifier here):

oauth_verifier = 12345
token = oauth.Token(request_token['oauth_token'],
request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
content = client.request(access_token_url,"POST")

access_token = dict(urlparse.parse_qsl(content[1]))

clean_access_token = {}
for key in access_token.keys():
    clean_access_token[key.decode('ascii')] = access_token[key].decode('ascii')
access_token = clean_request_token

token = oauth.Token(key=access_token['oauth_token'],secret=access_token['oauth_token_secret'])

client = oauth.Client(consumer, token)

response = client.request("http://api.linkedin.com/v1/companies/barclays")

This response has a 401 code, due to "The token used in the OAuth request has been revoked."

The underlying problems are:

  • I don't really get how APIs work, how they work with python, how authorisation works or how to know the api url I need.

In case relevant, I have experience web scraping (using requests plus beautiful soup to parse) but not with APIs.

1

1 Answers

12
votes

I eventually worked it out, posting here in case anyone comes this way. Before you invest time, I also found out that the freely available API now only allows you to access your own profile or company page. So you can write an app that allows a user to post to their own page, but you can't write something to grab data. See here:

LinkedIn API unable to view _any_ company profile

Anyway, to get the limited API working, you need to:

  • Create a LinkedIn account, create an application and add a redirect URL to your application page (I used http://localhost:8000). This doc says how to set up the app: https://developer.linkedin.com/docs/oauth2
  • Following the steps in the above link, but in python, you make a request to gain an "access code".

    html = requests.get("https://www.linkedin.com/oauth/v2/authorization", params = {'response_type':'code','client_id':client_id, 'redirect_uri':'http://localhost:8000', 'state':'somestring'})

  • print html.url to get a huge link - click on it. You'll be asked to login and allow access, and then you'll be redirected to your redirect url. There'll be nothing there, but the url will have a long "access code" on the end of it. Pull this out and send it to LinkedIn with a Post request:

    token = requests.post('https://www.linkedin.com/oauth/v2/accessToken', data = {'grant_type':'authorization_code','code':access_code, 'redirect_uri':'http://localhost:8000', 'client_id':client_id,'client_secret':client_secret})

  • token.content will contain an "access_token". This is what is needed to access the API. e.g. to access your own profile:

    headers = {'x-li-format': 'json', 'Content-Type': 'application/json'} params = {'oauth2_access_token': access_token}

    html = requests.get("https://api.linkedin.com/v1/people/~",headers=headers,params = params)

Hopefully that's useful to someone starting from scratch, the info is mostly out there but there are lots of assumed steps (like how to use the access token with requests).