linkedin-api: Unofficial LinkedIn API Client
linkedin-api is an unofficial Python client for interacting with LinkedIn's internal APIs. It allows developers to programmatically access various LinkedIn features, such as profiles, connections, and company data, by simulating browser behavior. As it uses unofficial APIs, it is prone to breaking changes and requires careful usage to avoid account issues. The current version is 2.3.1, with releases occurring as needed to address LinkedIn's frequent changes.
Common errors
-
Could not login: Check your email and password.
cause Incorrect username or password, or LinkedIn detected suspicious activity requiring manual intervention (e.g., CAPTCHA, 2FA challenge, or account lock).fixDouble-check your credentials. Try logging into LinkedIn manually in a browser to see if there's a security challenge. If 2FA is enabled, you might need to provide a session cookie instead of direct username/password. -
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
cause The API endpoint did not return valid JSON. This often happens if LinkedIn redirected the request to a CAPTCHA page, a 'something went wrong' page, or a block page instead of the expected data.fixThis usually indicates your account or IP is being challenged or temporarily blocked. Wait some time, reduce request frequency, or try with a different IP/account. Check the raw response content to understand what LinkedIn actually returned. -
requests.exceptions.HTTPError: 429 Client Error: Too Many Requests for url: ...
cause You have exceeded LinkedIn's rate limits, or your IP address has been temporarily throttled or blocked due to aggressive scraping behavior.fixImplement robust rate limiting and exponential backoff in your code. Introduce delays between API calls (e.g., `time.sleep()`). Consider distributing requests across multiple IP addresses or using a different API client if the issue persists. -
Login failed, please try again later.
cause LinkedIn's login flow or security measures have changed (e.g., CAPTCHA, 2FA, or HTML structure), preventing the unofficial API from successfully authenticating, or your credentials are incorrect.fixEnsure your credentials are correct, try updating the `linkedin-api` library to the latest version (`pip install --upgrade linkedin-api`), and try manually logging into LinkedIn in a browser to check for any interactive challenges. -
ModuleNotFoundError: No module named 'linkedin_api'
cause The `linkedin-api` Python package has not been installed in your current Python environment.fixInstall the library using pip: `pip install linkedin-api`.
Warnings
- breaking As an unofficial client, 'linkedin-api' is highly susceptible to breaking changes. LinkedIn frequently updates its internal APIs, which can cause methods to fail or return unexpected data. The library maintainers strive to keep it updated, but delays are possible.
- gotcha Frequent or aggressive use of the API can lead to your LinkedIn account being rate-limited, temporarily blocked, or even permanently restricted by LinkedIn's bot detection mechanisms. This includes rapid successive requests or scraping large amounts of data.
- gotcha Authentication with username/password can be unreliable, especially if your LinkedIn account has Two-Factor Authentication (2FA) enabled or if LinkedIn suspects bot activity, leading to CAPTCHA challenges or login failures.
Install
-
pip install linkedin-api
Imports
- Linkedin
from linkedin_api import Linkedin
Quickstart
import os
from linkedin_api import Linkedin
# It's highly recommended to use environment variables for credentials
USERNAME = os.environ.get('LINKEDIN_USERNAME', 'your_email@example.com')
PASSWORD = os.environ.get('LINKEDIN_PASSWORD', 'your_strong_password')
if not USERNAME or not PASSWORD:
print("Please set LINKEDIN_USERNAME and LINKEDIN_PASSWORD environment variables.")
else:
try:
api = Linkedin(USERNAME, PASSWORD)
# Example: Get a public profile by its LinkedIn URL or ID
# Replace 'ACoAAADm-wABuE77A8WbNlQoV-xT8D12345' with an actual profile ID
profile = api.get_profile('ACoAAADm-wABuE77A8WbNlQoV-xT8D12345')
print(f"Successfully fetched profile for {profile.get('firstName')} {profile.get('lastName')}")
# Example: Get posts from a specific person
# posts = api.get_profile_posts('ACoAAADm-wABuE77A8WbNlQoV-xT8D12345')
# print(f"Fetched {len(posts)} posts.")
except Exception as e:
print(f"An error occurred: {e}")