Get OAuth 2.0 Access Token For Linkedin Using Python3-linkedin


LinkedIn is a social networking service through which working professionals connect with each other. LinkedIn allows various kinds of information sharing among its connected users including posting of job profiles of individuals and job requirements of the corporations.

LinkedIn provides access to it services through well-defined APIs as well. The Python API written for LinkedIn is python3-linkedin.

Using python3-linkedin API any App written in Python can avail the services extended by LinkedIn, which include getting access to LinkedIn using OAuth 2.0, getting the basic profile or full profile of a user, getting the list of connections of a LinkedIn user, using the LinkedIn search, LinkedIn Groups, Company Profiles, Content sharing and Network updates.

This article explains how a Web Application written in Python can connect to LinkedIn using OAuth 2.0 and get an access token, which can be used for accessing LinkedIn services.

Retrieving LinkedIn user profile using python3-linkedin:

The python examples used in this article are developed using HTML, CherryPy the Python based web framework and python3-linkedin API.  Getting an OAUTH 2.0 access token to the LinkedIn services by a web application using the Python API python3-linkedin involves the following steps:

  1. By passing the Client Id, Client Secret and the Return-URL of the web application, an authentication object is created. The Client Id and the Client Secret are obtained by registering the web Application in the LinkedIn developer console at
  2. An authorization URL is obtained from authentication object. The web application redirects to this LinkedIn Authorization URL.
  3. LinkedIn displays the access grant page, where it asks for the LinkedIn authentication along with the access grant to LinkedIn services.
  4. After access is granted, LinkedIn redirects to the Return-URL provided in step one, along with the authorization code in query string.
  5. The call back Return-URL is served by the Python code using CherryPy.
  6. The authorization code is received from the query string, from the query string variable named code.
  7. The received authorization code is set to the authentication object received in step one.
  8. Once the authorization code is set to the authentication object the OAUTH 2.0 access token is received from the authentication object
  9. The OAUTH 2.0 access token is set to the authentication object obtained in step one.
  10. Equipped with the access token, an application object can be created and the LinkedIn services can be used.


  • The control flow the example given here starts from the homepage served by the CherryPy.
  • CherryPy can be started with the following command line:

python -m cherrypy.linkedintest.webapp_linkedinaccess

  • The file has the following classes:
    • LinkedInAccessForm
    • WebAppHome
  • The class WebAppHome serves two purposes:
    1. To serve the home page of the Web Application that needs the LinkedIn access.
    2. After the successful access grant, LinkedIn redirects to the return URL. The logic to serve this URL is served by the WebAppHome.Remember, the Return-URL that is provided in the example is http://localhost:8080 which is served by the class WebAppHome hosted in CherryPy.

# Example Web Application for LinkedIn written in Python using python3-LinkedIn and CherryPy

# imports

import os.path

import json

import io

# import CherryPy

import cherrypy


# import the python3-linkedin api wrapper

from linkedin import linkedin

from linkedin import server

#Authorize an user of the web application to access LinkedIn services in using OAuth2

appKey      = "xxxxxxxxxxxxxx"
appSecret   = "xxxxxxxxxxxxxxxx"
returnURL   = "http://localhost:8080"
auth        = linkedin.LinkedInAuthentication( appKey,
authorizationURL = auth.authorization_url 

# class for the LinkedIn Authorization
class LinkedInAuthorization:
    def index(self):
        # redirect to LinkedIn authorization URL
        raise cherrypy.HTTPRedirect(authorizationURL)

# Class that serves the home page i.e.,localhost:8080
# In case of authorization complete, prints the linkedin authorization code and 
# OAuth 2.0 authorization token
class WebAppHome:
    def index(self, *args, **kwargs):

        content = io.StringIO()

        # Home page case - after authorization
        if len(kwargs) > 0:
            authCode = kwargs["code"]
            auth.authorization_code = authCode

            content.write("<b>Authorization code:</b>")

            content.write("<b>Authorization token:</b>")
            token = auth.get_access_token();

            content.write("<b>Access token is valid for:</b>")

            output = content.getvalue()
            return [bytes(output, 'utf-8')]                    
        # Home page case - before authorization
        content.write('For the test application to authorize Linkedin access')
        content.write('<h1>Test web application with Linkedin Access</h1>')
        content.write('<p>Test web application that gets Linkedin authorization from the ') 
        content.write('user and prints the OAUTH 2.0 access token</p>')
        content.write('<form action="for_linkedin_access" method="post">')
        content.write('<input type="submit" value="Authorize Test Application for Linkedin Access">')
        output = content.getvalue()
        return [bytes(output, 'utf-8')] 

conf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')

if __name__ == '__main__':
    cherrypy.tree.mount(WebAppHome(),                   '/', config=conf)
    cherrypy.tree.mount(LinkedInAuthorization(),        '/for_linkedin_access', config=conf)



Sample web app that gets LinkedIn access using OAUTH2.0

Linkedin Authorization Page - Sample web application




Authorization code:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Authorization token:
Access token is valid for:

Copyright 2022 ©