Hey there, fellow dev! Ready to tap into the power of LinkedIn's API using Python? You're in the right place. We'll be using the nifty python-linkedin package to make our lives easier. Let's dive in and get your LinkedIn integration up and running in no time!
Before we jump into the code, make sure you've got:
First things first, let's get that python-linkedin package installed:
pip install python-linkedin
Easy peasy, right?
Now, let's get you authenticated:
from linkedin import linkedin # Your app's info CLIENT_ID = 'your_client_id' CLIENT_SECRET = 'your_client_secret' RETURN_URL = 'http://localhost:8000' authentication = linkedin.LinkedInAuthentication( CLIENT_ID, CLIENT_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values() ) # Print the auth URL print(authentication.authorization_url)
Visit that URL, authorize your app, and grab the code from the redirect URL.
Let's get that LinkedIn client initialized and fetch some profile info:
code = 'the_code_from_redirect_url' authentication.authorization_code = code token = authentication.get_access_token() # Initialize the LinkedIn client application = linkedin.LinkedInApplication(token=token) # Fetch profile info profile = application.get_profile() print(profile)
Boom! You're now connected to LinkedIn's API.
Want to do more? Here are some cool things you can try:
# Search for people search_results = application.search_profile( selectors=[{'people': ['first-name', 'last-name']}], params={'keywords': 'python developer'} ) # Get company info company = application.get_companies(company_ids=[1035]) # Post an update application.submit_share('Check out this cool LinkedIn API integration!')
LinkedIn's API has rate limits, so be cool and respect them. Implement some error handling:
import time from linkedin.exceptions import LinkedInError def api_call_with_retry(func, max_retries=3, *args, **kwargs): for i in range(max_retries): try: return func(*args, **kwargs) except LinkedInError as e: if i == max_retries - 1: raise if 'throttle' in str(e).lower(): time.sleep(2 ** i) # Exponential backoff else: raise
Let's put it all together with a simple job search tool:
def search_jobs(keywords, location): jobs = application.search_job( selectors=[{'jobs': ['id', 'customer-job-code', 'posting-date']}], params={'keywords': keywords, 'location': location} ) return jobs python_jobs = search_jobs('python', 'San Francisco') for job in python_jobs['jobs']['values']: print(f"Job ID: {job['id']}, Posted on: {job['posting-date']}")
And there you have it! You're now equipped to build some awesome LinkedIn integrations. Remember to check out the python-linkedin docs for more details, and happy coding!