LinkedIn API Python Client
The official Python client library for LinkedIn APIs. It provides a thin client for making requests to LinkedIn APIs, handling the complexities of the Rest.li framework, formatting requests, and managing authentication (OAuth2). The library aims to reduce the difficulty of interacting with LinkedIn's robust but complex API protocol. The current version is 0.3.0, and it maintains an active release cadence.
Warnings
- beta This API client library is officially in beta and is subject to change. It may contain bugs, errors, or other issues. Use in production environments should be approached with caution, and users should be prepared for unexpected behavior.
- gotcha There are multiple Python libraries for LinkedIn, including a popular unofficial one (`linkedin-api` by Tom Quirk) that uses web scraping/simulation. This official `linkedin-api-client` library requires proper OAuth2 authentication and interaction via the LinkedIn Developer Portal, while the unofficial one may work by using a username/password, potentially violating LinkedIn's Terms of Service.
- breaking LinkedIn's underlying APIs are frequently updated with monthly versions, and these can introduce breaking changes (e.g., changes to resource paths, required fields, or deprecation of features). The client library aims to abstract some of this, but direct API calls may still be affected.
- gotcha Authentication for the official LinkedIn API client relies on OAuth2 flows (2-legged for applications, 3-legged for user-specific actions). Obtaining valid access tokens (especially 3-legged) requires a pre-requisite setup in the LinkedIn Developer Portal, including creating an application, requesting API product access, and configuring redirect URIs.
- gotcha Some GitHub issues indicate that pagination may be broken with certain API versions, particularly for cursor-based pagination.
Install
-
pip install linkedin-api-client
Imports
- RestliClient
from linkedin_api.clients.restli.client import RestliClient
- AuthClient
from linkedin_api.clients.auth.auth_client import AuthClient
- Linkedin
from linkedin_api.clients.restli.client import RestliClient
Quickstart
import os
from linkedin_api.clients.restli.client import RestliClient
# Get your 3-legged access token from environment variables or a secure store.
# This token must be generated via the LinkedIn Developer Portal's OAuth 2.0 flow.
ACCESS_TOKEN = os.environ.get('LINKEDIN_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE')
if not ACCESS_TOKEN or ACCESS_TOKEN == 'YOUR_ACCESS_TOKEN_HERE':
raise ValueError("Please set the LINKEDIN_ACCESS_TOKEN environment variable or replace the placeholder.")
restli_client = RestliClient()
try:
# Example: Fetch current user's information
response = restli_client.get(
resource_path="/userinfo",
access_token=ACCESS_TOKEN
)
print("Successfully fetched user info:")
print(response.entity)
except Exception as e:
print(f"An error occurred: {e}")