Overview:
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:
- 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 developer.linkedin.com.
- An authorization URL is obtained from authentication object. The web application redirects to this LinkedIn Authorization URL.
- LinkedIn displays the access grant page, where it asks for the LinkedIn authentication along with the access grant to LinkedIn services.
- After access is granted, LinkedIn redirects to the Return-URL provided in step one, along with the authorization code in query string.
- The call back Return-URL is served by the Python code using CherryPy.
- The authorization code is received from the query string, from the query string variable named code.
- The received authorization code is set to the authentication object received in step one.
- Once the authorization code is set to the authentication object the OAUTH 2.0 access token is received from the authentication object
- The OAUTH 2.0 access token is set to the authentication object obtained in step one.
- Equipped with the access token, an application object can be created and the LinkedIn services can be used.
Example:
- 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 webapp_linkedinaccess.py has the following classes:
- LinkedInAccessForm
- WebAppHome
- The class WebAppHome serves two purposes:
- To serve the home page of the Web Application that needs the LinkedIn access.
- 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" # class for the LinkedIn Authorization # Class that serves the home page i.e.,localhost:8080 content = io.StringIO() # Home page case - after authorization content.write("<b>Authorization code:</b>") content.write("<b>Authorization token:</b>") content.write("<b>Access token is valid for:</b>") output = content.getvalue() conf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': |
Output:
Authorization code:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |