For any API call to the Linkedin API, you will need to have your user URN at hand. In the next script, we will learn how to query your user information using the LinkedIn API.
This post is part of the complete guide on how to use the LinkedIn API with Python
- Get Your OAuth Credentials
- Authenticate Using OAuth 2.0
- Get Your Own User Information
- Simple Steps to Find Any Company ID on LinkedIn
- How to Post On LinkedIn API With Python
- How to Scrape LinkedIn Jobs with Python
It will be used to get the LinkedIn user ID. The user ID will be used to generate the urn needed to publish content using the API.
This query will return a dictionary like this:
{
'localizedLastName': 'Chouinard',
'profilePicture': {
'displayImage': 'urn:li:digitalmediaAsset:XXXXXXX'
},
'firstName': {
'localized': {
'fr_FR': 'Jean-Christophe',
'en_US': 'Jean-Christophe'
},
'preferredLocale': {
'country': 'US', 'language': 'en'
}
},
'lastName': {
'localized': {
'fr_FR': 'Chouinard', 'en_US': 'Chouinard'
},
'preferredLocale': {
'country': 'US', 'language': 'en'
}
},
'id': 'XXXXXXXX',
'localizedFirstName': 'Jean-Christophe'
}
Import Packages
Let’s install requests and import it.
$ pip install requests
import requests
Then, we will need to import the auth()
and headers()
functions from the ln_oauth
module we created in the post to authenticate to the LinkedIn API using the OAuth 2.0.
from ln_oauth import auth, headers
Make the Request
Make a GET
request to the Profile API using the https://api.linkedin.com/v2/me
url. We will need to pass the headers to the function using the headers()
function from ln_oauth
.
def user_info(headers):
'''
Get user information from Linkedin
'''
response = requests.get('https://api.linkedin.com/v2/me', headers = headers)
user_info = response.json()
return user_info
Authorize the API
Now it is time to authorize the API.
credentials = 'credentials.json'
access_token = auth(credentials) # Authenticate the API
If you don’t know how to get the credentials, just read the post on how to get your LinkedIn OAuth credentials.
Create the Headers
The headers()
function from ln_oauth.py
will create the headers to pass to the GET
request to authorize the API call.
The headers have this structure:
{'Authorization': 'Bearer <ACCESS_TOKEN>',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0'}
Run the Code
if __name__ == '__main__':
credentials = 'credentials.json'
access_token = auth(credentials) # Authenticate the API
headers = headers(access_token) # Make the headers to attach to the API call.
user_info = user_info(headers) # Get user info
print(user_info)
The if name equals main line checks whether you are running the module or importing it. If you are importing it, user_info()
will not run.
Full Code
import requests
from ln_oauth import auth, headers
def user_info(headers):
'''
Get user information from Linkedin
'''
response = requests.get('https://api.linkedin.com/v2/me', headers = headers)
user_info = response.json()
return user_info
if __name__ == '__main__':
credentials = 'credentials.json'
access_token = auth(credentials) # Authenticate the API
headers = headers(access_token) # Make the headers to attach to the API call.
user_info = user_info(headers) # Get user info
print(user_info)
Troubleshooting
If this does not work, you might have a problem with your access token permissions. In that case, you can find out by using the Token Inspector.
That’s it, you now have extracted your user information using the LinkedIn API and Python.
The next step is to learn How to post on the LinkedIn API with Python.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. Writer in Python, Information Retrieval, SEO and machine learning. Guest author at SearchEngineJournal, SearchEngineLand and OnCrawl.