I am trying to computationally access my own LinkedIn profile via API to download my own posts. There are three recent Python wrappers to access my profile, e.g. linkedin-sdk, pawl, LinkedIn V2. However, I have been unable to make them work. The problem is the authentication. I have seen the famous LinkedIn-API wrapper, but its authentication process is complex and difficult probably due to LinkedIn changing its authentication process and access scope.
Based on this tutorial from last year I have been able to access my own profile to view my name, country, language and id.
import requests
#get access_token by post with user & password
#Step 1 - GET to request for authentication
def get_auth_link():
URL = "https://www.linkedin.com/oauth/v2/authorization"
client_id= 'XXXX'
redirect_uri = 'http://localhost:8080/login'
scope='r_liteprofile'
PARAMS = {'response_type':'code', 'client_id':client_id, 'redirect_uri':redirect_uri, 'scope':scope}
r = requests.get(url = URL, params = PARAMS)
return_url = r.url
print('Please copy the URL and paste it in browser for getting authentication code')
print('')
print(return_url)
get_auth_link()
# Make a POST request to exchange the Authorization Code for an Access Token
import json
def get_access_token():
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'OAuth gem v0.4.4'}
AUTH_CODE = 'XXXX'
ACCESS_TOKEN_URL = 'https://www.linkedin.com/oauth/v2/accessToken'
client_id= 'XXXX'
client_secret= 'XXXX'
redirect_uri = 'http://localhost:8080/login'
PARAM = {'grant_type': 'authorization_code',
'code': AUTH_CODE,
'redirect_uri': redirect_uri,
'client_id': client_id,
'client_secret': client_secret}
response = requests.post(ACCESS_TOKEN_URL, data=PARAM, headers=headers, timeout=600)
data = response.json()
print(data)
access_token = data['access_token']
return access_token
get_access_token()
access_token = 'XXXX'
def get_profile(access_token):
URL = "https://api.linkedin.com/v2/me"
headers = {'Content-Type': 'application/x-www-form-urlencoded','Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
response = requests.get(url=URL, headers=headers)
print(response.json())
get_profile(access_token)
As soon as I change the scope from r_liteprofile to r_basicprofile I get the an unauthorized_scope_error: r_basicprofile is not authorised for your application. In my developpers page I have the scopesr_emailaddress, r_liteprofile and w_member_social authorised. But only r_liteprofile works. From what I understand from the LinkedIn documentation, comments cannot be downloaded?
So the big question really is, can comments be downloaded via API?
Bots or scrapers are not an option as they require explicit permission from LinkedIn, which I do not have.
Up-date: so no illegal solutions please. I knew before I have written this post that they exist.
Thanks for your help!