DEV Community

Chidozie C. Okafor
Chidozie C. Okafor

Posted on • Originally published at doziestar.Medium on

Sample code snippets for working with LinkedIn API in python.

Welcome to this complete guide on using the LinkedIn API with Python. I will try to go as deep as possible and avoid using libraries which offer lots of abstractions and prevent you from having a deeper understanding of what happens under the hood.

We will divide this guide into two sections.

  1. Authentication: To interact successfully with LinkedIn, we need to have a way to tell LinkedIn who we are, this will enable LinkedIn to check if we have the right access to access such content. Also, it helps to protect the users from identity fraud. In modern software that uses api to interact with each other, there are many ways to check if the user is authorized. The most popular is the use of an access Token to check if the user is given access, which is one of the ways Linkedin uses to check who is making the request.
  2. Getting Resources: Here we will try to use the access token we got in above to ask for any information from LinkedIn and expect the right response or an error if we don’t the right permission to communicate.

Table of contents

  1. Signing up to linkedin Oauth (requesting access_token)
  2. requesting user pieces of information
  3. searching for jobs
  4. searching for companies
  5. searching for users

Authentication (requesting access_token: “Oauth2.0”)

The first step to use the LinkedIn API is to create an app in the LinkedIn Developer Tool. In this tutorial, we will see how you can get your OAuth 2.0 credentials to use the LinkedIn API.

when you are done with your account creation, it’s now time to copy your client_id and client_secret.

Now let’s code some python and interact with our LinkedIn API. Firstly we need to find a way to store some useful information we will need for recurring requests, such as the client_id and client_secret that we use for authentication, also we will try to store our accessToken somewhere so that we can always have access to them.

Since our aim is to keep things extremely clear and simple. we won’t do anything complicated in storing those useful pieces of information. so let’s create a file name credentials.json. this file will hold all our user data, and make it possible for us to read and store some pieces of information.

{
“client_id”: “77cbb96oqhael6”,

“client_secret”: “”, // this is a secret

“redirect_uri”: “http://127.0.0.1:8000/auth/callback",
}
credentials.json
Enter fullscreen mode Exit fullscreen mode

This file now holds our client_id, client_secret and our callback URL. callback URL is a link that LinkedIn will call automatically anytime we complete the auth flow.Read Credentials

Let’s now create a function that will enable us to read this file.

import json

def read_creds(filename):
    with open(filename) as f:

    credentials = json.load(f)

    return credentials

creds = read_creds("credentials.json")

client_id, client_secret = creds["client_id"], creds["client_secret"]

redirect_uri = creds["redirect_uri"]
Enter fullscreen mode Exit fullscreen mode

The only thing we have done is read a file and grab the items of this file and assign them to a variable. we are more specific here by loading the JSON data inside the file, so this option might not work if you use any other structure like YAML.Authorize The API

To authorize the API, you will need to generate a CSRF token to prevent cross-site request forgery. The create_CSRF_token() function does this.

It creates a random string of letters to use as the CSRF Token.

import random

def create_CSRF_token():

    letters = string.ascii_lowercase

    token = "".join(random.choice(letters) for i in range(20))

    return token
Enter fullscreen mode Exit fullscreen mode

Now let’s define functions that open our web browser, parse the data we got from the redirect URL, save our access token and set request headers.

def open_url(url):

    import webbrowser

    print(url)

    webbrowser.open(url)

def parse_redirect_uri(redirect_response):

    from urllib.parse import parse_qs, urlparse

    url = urlparse(redirect_response)

    url = parse_qs(url.query)

    return url["code"][0]

def save_token(filename, data):

    data = json.dumps(data, indent=4)

    with open(filename, "w") as f:

    f.write(data)

def headers(access_token):

    headers = {

       "Authorization": f"Bearer {access_token}",

       "cache-control": "no-cache",

       "X-Restli-Protocol-Version": "2.0.0",

       }

    return headers
Enter fullscreen mode Exit fullscreen mode

Then, the authorize() function says what it does. It will open the authentication URL. Once authorized, it’ll redirect to the redirect URI was given.

def authorize(api_url, client_id, client_secret, redirect_uri):

    # Request authentication URL

    csrf_token = create_CSRF_token()

    params = {

    "response_type": "code",

    "client_id": client_id,

    "redirect_uri": redirect_uri,

    "state": csrf_token,

    "scope": "r_liteprofile,r_emailaddress",

   }

    response = requests.get(f"{api_url}/authorization", params=params)

     print(f"""

     The Browser will open to ask you to authorize the credentials.\n

    Since we have not set up a server, you will get the error:\n

    This site can’t be reached. localhost refused to connect.\n

    This is normal.\n

    You need to copy the URL where you are being redirected to.\n

   """)

    open_url(response.url)

    redirect_response = input("Paste the full redirect URL here:")

    auth_code = parse_redirect_uri(redirect_response)

    return auth_code
Enter fullscreen mode Exit fullscreen mode

Now only need one function that combines all these functions together and executes your codes once. let’s call it auth.

The logic behind the auth() function goes like this:

  • Run the Authentication.
  • The first time the function runs, the browser opens asking you to authenticate.
  • You will have to manually paste the redirect URI in the prompt.
  • The URL will be parsed to extract the access token.
  • It will save the access token
  • Next time, it will use the access token instead of asking you to authenticate.
def auth(credentials):

    creds = read_creds(credentials)

    print(creds)

    client_id, client_secret = creds["client_id"], creds["client_secret"]

    redirect_uri = creds["redirect_uri"]

    # api_url = "https://www.linkedin.com/oauth/v2"

    api_url = "https://www.linkedin.com/uas/oauth2"

    if "access_token" not in creds.keys():

      args = client_id, client_secret, redirect_uri

      auth_code = authorize(api_url, *args)

      access_token = refresh_token(auth_code, *args)

      creds.update({"access_token": access_token})

      save_token(credentials, creds)

    else:

      access_token = creds["access_token"]

      return access_token
Enter fullscreen mode Exit fullscreen mode

holla!! our authentication function is now ready. Let’s run…..

if __name__ == " __main__":
    credentials = "credentials.json"

    access_token = auth.auth(credentials)
Enter fullscreen mode Exit fullscreen mode

This what we got

It also opens our browser automatically

After authorization, LinkedIn redirects us to the call back we specify at the point of creating the app.

When we pass the URL to our terminal, it makes a request to LinkedIn and demands access_token, which Linkedin will verify the code and respond with an access_token for us to use to get information from Linkedin.

This is the result, and if we go back to our credentials.json, we will find out that our file is updated with access_token.

Now that we have access to our acces_token, we can now start asking LinkedIn API for information.

Getting Resources (Making Linkedin API Call With Oauth2.0)

Let’s start by getting our personal information from LinkedIn, we will switch to using an api client to make this request.

Firstly we will need to copy our token and pass it to the authorization header and prefix it with Bearer.

when we pass on this information, we can now expect a response. let’s check out this response.

Look at that, LinkedIn gave us my information. We can customize this response further by passing different query parameters. Let’s try to get my emails.

Since our focus is on showing you how we can interact with python without using the library. Let’s use FastAPI, a python web framework to make requests.

let’s continue later….

Top comments (0)