Skip to main content

Python LinkedIn V2

Python interface to the LinkedIn API V2

LinkedIn

This library provides a pure Python interface to the LinkedIn Profile and Connections REST APIs.

LinkedIn provides a service that lets people bring their LinkedIn profiles and networks with them to your site or application via their OAuth based API. This library provides a lightweight interface over a complicated LinkedIn OAuth based API to make it for python programmers easy to use.

Installation

You can install python-linkedin-v2 library via pip:

$ pip install python-linkedin-v2

Authentication

The LinkedIn REST API now supports the OAuth 2.0 protocol for authentication. This package provides a full OAuth 2.0 implementation for connecting to LinkedIn as well as an option for using an OAuth 1.0a flow that can be helpful for development purposes or just accessing your own data.

HTTP API example

Set LINKEDIN_API_KEY and LINKEDIN_API_SECRET, configure your app to redirect to http://localhost:8080/code, then execute:

  1. http_api.py
  2. Visit http://localhost:8080 in your browser, curl or similar
  3. A tab in your browser will open up, give LinkedIn permission there
  4. You'll then be presented with a list of available routes, hit any, e.g.:
  5. curl -XGET http://localhost:8080/get_profile

Developer Authentication

To connect to LinkedIn as a developer or just to access your own data, you don't even have to implement an OAuth 2.0 flow that involves redirects. You can simply use the 4 credentials that are provided to you in your LinkedIn appliation as part of an OAuth 1.0a flow and immediately access your data. Here's how:

from linkedin_v2 import linkedin

# Define CONSUMER_KEY, CONSUMER_SECRET,  
# USER_TOKEN, and USER_SECRET from the credentials 
# provided in your LinkedIn application

# Instantiate the developer authentication class

authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET, 
                                                          USER_TOKEN, USER_SECRET, 
                                                          RETURN_URL, linkedin.PERMISSIONS.enums.values())

# Pass it in to the app...

application = linkedin.LinkedInApplication(authentication)

# Use the app....

application.get_profile()

Production Authentication

In order to use the LinkedIn OAuth 2.0, you have an application key and application secret. You can get more detail from here.

For debugging purposes you can use the credentials below. It belongs to my test application. Nothing's harmful.

KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
SECRET = 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'

You can also get those keys from here.

LinkedIn redirects the user back to your website's URL after granting access (giving proper permissions) to your application. We call that url RETURN URL. Assuming your return url is http://localhost:8000, you can write something like this:

from linkedin_v2 import linkedin

API_KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
API_SECRET = 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'
RETURN_URL = 'http://localhost:8000'

authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values())
# Optionally one can send custom "state" value that will be returned from OAuth server
# It can be used to track your user state or something else (it's up to you)
# Be aware that this value is sent to OAuth server AS IS - make sure to encode or hash it
#authorization.state = 'your_encoded_message'
print authentication.authorization_url  # open this url on your browser
application = linkedin.LinkedInApplication(authentication)

When you grant access to the application, you will be redirected to the return url with the following query strings appended to your RETURN_URL:

"http://localhost:8000/?code=AQTXrv3Pe1iWS0EQvLg0NJA8ju_XuiadXACqHennhWih7iRyDSzAm5jaf3R7I8&state=ea34a04b91c72863c82878d2b8f1836c"

This means that the value of the authorization_code is AQTXrv3Pe1iWS0EQvLg0NJA8ju_XuiadXACqHennhWih7iRyDSzAm5jaf3R7I8. After setting it by hand, we can call the .get_access_token() to get the actual token.

authentication.authorization_code = 'AQTXrv3Pe1iWS0EQvLg0NJA8ju_XuiadXACqHennhWih7iRyDSzAm5jaf3R7I8'
authentication.get_access_token()

After you get the access token, you are now permitted to make API calls on behalf of the user who granted access to you app. In addition to that, in order to prevent from going through the OAuth flow for every consecutive request, one can directly assign the access token obtained before to the application instance.

application = linkedin.LinkedInApplication(token='AQTFtPILQkJzXHrHtyQ0rjLe3W0I')

Profile API

The Profile API returns a member's LinkedIn profile. For more information, check out the documentation.

application.get_profile()
{
  "id": "yrZCpj2Z12",
  "firstName": {
    "localized": {
      "en_US": "Bob"
    },
    "preferredLocale": {
      "country": "US",
      "language": "en"
    }
  },
  "lastName": {
    "localized": {
      "en_US": "Smith"
    },
    "preferredLocale": {
      "country": "US",
      "language": "en"
    }
  },
  "location": {
    "countryCode": "us",
    "postalCode": "94101",
    "standardizedLocationUrn": "urn:li:standardizedLocationKey:(us,94101)"
  },
  "positions": {
    "660879450": {
      "companyName": {
        "localized": {
          "en_US": "LinkedIn"
        },
        "preferredLocale": {
          "country": "US",
          "language": "en"
        }
      },
      "id": 660879450,
      "title": {
        "localized": {
          "en_US": "Staff Software Engineer"
        },
        "preferredLocale": {
          "country": "US",
          "language": "en"
        }
      }
    }
  },
  "headline": {
    "localized": {
      "en_US": "API Enthusiast at LinkedIn"
    },
    "preferredLocale": {
      "country": "US",
      "language": "en"
    }
  }
}

Connections API

The Connections API returns a list of 1st degree connections for a user who has granted access to their account. For more information, you check out its documentation.

To fetch your connections, you simply call .get_connections() method with proper GET querystring:

application.get_connections()
{
  "elements": [
    {
      "to": "urn:li:person:9HfhE6QlBz"
    }
  ],
  "paging": {
    "total": 1,
    "count": 50,
    "start": 0,
    "links": []
  }
}

application.get_connections(totals_only=True)
{
   "elements":[

   ],
   "paging":{
      "total":303,
      "count":0,
      "start":0
   }
}

Throttle Limits

LinkedIn API keys are throttled by default. You should take a look at the Throttle Limits Documentation to get more information about it.

Metadata

Release files for python-linkedin-v2 0.9.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for python-linkedin-v2 0.9.5
File Interpreter ABI Platform Actions
python_linkedin_v2-0.9.5-py2-none-any.whl Python 2 none any Details for python_linkedin_v2-0.9.5-py2-none-any.whl

Release files / python_linkedin_v2-0.9.5-py2-none-any.whl

Download URL python_linkedin_v2-0.9.5-py2-none-any.whl
Size 10.2 kB
Tags Python 2
SHA-256 checksum
How to use checksums
9c9cd5b48340ee55af1761c6d79cefbdba39e145458c373c16b88dea93dc235a
BLAKE2b-256 checksum
How to use checksums
c9526945fd9448bf735b5e12c9b4c8ec09b8300a684a1578797ec689c7d1979f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.15

Security for python-linkedin-v2 0.9.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page