This library provides a thin Python client for making requests to LinkedIn APIs, utilizing the Python requests HTTP client library. LinkedIn's APIs are built on the Rest.li framework with additional LinkedIn-specific constraints, which results in a robust yet complex protocol that can be challenging to implement correctly.
This library helps reduce this complexity by formatting requests correctly, providing proper request headers, and providing interfaces to develop against for responses. The library also provides an auth client for inspecting, generating, and refreshing access tokens, along with other helpful utilities.
:warning: This API client library is currently in beta and is subject to change. It may contain bugs, errors, or other issues that we are working to resolve. Use of this library is at your own risk. Please use caution when using it in production environments and be prepared for the possibility of unexpected behavior. We welcome any feedback or reports of issues that you may encounter while using this library.
Features
Generic support for all Rest.li methods used in LinkedIn APIs
Supports Rest.li protocol version 2.0.0
Provide interfaces for request options/response payloads
Request access to the Sign In With LinkedIn API product. This is a self-serve product that will be provisioned immediately to your application.
Generate a 3-legged access token using the Developer Portal token generator tool, selecting the r_liteprofile scope.
Simple API Request Example
Here is an example of using the client to make a simple GET request to fetch the current user's profile. This requires a 3-legged access token with the "r_liteprofile" scope, which is included with the Sign In With LinkedIn API product.
Here is a more non-trivial example to find ad accounts by some search criteria. This requires a 3-legged access token with the "r_ads" scope, which is included with the Advertising APIs product.
The "search" query parameter in this case is not a primitive, but a complex object, which we represent as a dictionary. The client will handle the correct URL-encoding. This is a versioned API call, so we also need to provide the version string in the "YYYYMM" format.
There are more examples of using the client in /examples directory.
API Reference
class RestliClient
The Rest.li API client defines instance methods for all the Rest.li methods which are used by LinkedIn APIs. All calls are blocking.
Rest.li defines a standard set of methods that can operate on a resource, each of which maps to an HTTP method. Depending on the resource, some Rest.li methods are not applicable or not implemented. Read the API docs to determine what Rest.li method is applicable and the relevant request parameters.
Constructor
An instance of the API client must be created before using. This creates a session object that will be used for making all subsequent requests.
from linkedin_api.clients.restli.client import RestliClient
restli_client = RestliClient()
The Requests library session object is accessible to configure any additional global settings (e.g. configuring event hooks).
The session object used for making http requests. This is exposed to allow for additional configuration (e.g. adding custom request/response event hooks).
Methods
Base Request Parameters
All Rest.li request methods of the API client support the following request parameters:
Parameter
Type
Required?
Description
resource_path
str
Yes
The resource path after the base URL, beginning with a forward slash. If the path contains keys, add curly-brace placeholders for the keys and specify the path key-value map in the path_keys argument.
The access token that should provide the application access to the specified API
path_keys
Dict[str,Any]
No
If there are path keys that are part of the resource_path argument, the key placeholders must be specified in the provided path_keys map. The path key values can be strings, numbers, or objects (dictionaries), and these will be properly encoded.
A map of query parameters. The query parameter values (strings, lists, objects) will be correctly encoded by this method, so these should not be encoded.
version_string
str
No
An optional version string of the format "YYYYMM" or "YYYYMM.RR". If specified, the version header will be passed and the request will use the versioned APIs base URL
get (resource_path, access_token, path_keys=None, query_params=None, version_string=None)
Makes a Rest.li GET request to fetch the specified entity on a resource. This method will perform query tunneling if necessary.
The Rest.li batch finder name (the value of the "bq" query parameter). This will be added to the request query parameters.
finder_criteria
Tuple[str, List[Dict[str,Any]]]
Yes
The required batch finder criteria information. This is a tuple with the first value being the batch finder criteria parameter name. The second value is the list of finder param objects. The batch finder results are correspondingly ordered according to this list. The batch finder criteria will be encoded and added to the request query parameters.
Makes a Rest.li PARTIAL_UPDATE request to update part of an entity. Directly specify the patch object to send in the request.
Note: While the Rest.li protocol supports very granular patch objects with setting and deletion of nested properties, most LinkedIn APIs only support partial update on the top-level fields of an entity.
Makes a Rest.li ACTION request to perform an action on a specified resource. This method is flexible and generally used when the action does not fit within the standard behavior defined by the other Rest.li methods.
All Rest.li request methods of the API client return a response object subclassed from BaseRestliResponse, containing standard response data, along with the original, raw response object.
Properties
Type
Description
status_code
int
Response status code
url
str
The final URL location of the response
headers
CaseInsensitiveDict
A case-insensitive dictionary of response headers
response
Response
The raw requests.Response object
class Paging
Paging metadata class
Properties
Type
Description
start
int
The start index of returned results (zero-based index)
While we recommend using any of several popular, open-source libraries for robustly managing OAuth 2.0 authentication, we provide a basic Auth Client as a convenience for testing APIs and getting started.
Constructor
from linkedin_api.clients.auth.client import AuthClient
auth_client = AuthClient(client_id=MY_CLIENT_ID, client_secret=MY_CLIENT_SECRET, redirect_url=MY_REDIRECT_URL)
Parameter
Type
Required?
Description
client_id
str
Yes
Client ID of your developer application. This can be found on your application auth settings page in the Developer Portal.
client_secret
str
Yes
Client secret of your developer application. This can be found on your application auth settings page in the Developer Portal.
redirect_url
str
No
If your integration will be using the authorization code flow to obtain 3-legged access tokens, this should be provided. This redirect URL must match one of the redirect URLs configured in the app auth settings page in the Developer Portal.
Properties
Property
Description
session
The session object used for making http requests. This is exposed to allow for additional configuration (e.g. adding custom request/response event hooks).
Methods
generate_member_auth_url (scopes, state=None)
Generates the member authorization URL to direct members to. Once redirected, the member will be presented with LinkedIn's OAuth consent page showing the OAuth scopes your application is requesting on behalf of the user.
Parameters:
Parameter
Type
Required?
Description
scopes
List[str]
Yes
An array of OAuth scopes (3-legged member permissions) your application is requesting on behalf of the user.
state
str
No
An optional string that can be provided to test against CSRF attacks.
Return value:
The member authorization URL string
exchange_auth_code_for_access_token (code)
Exchanges an authorization code for a 3-legged access token. After member authorization, the browser redirects to the provided redirect URL, setting the authorization code on the code query parameter.
Parameters:
Parameter
Type
Required?
Description
code
str
Yes
The authorization code to exchange for an access token
Exchanges a refresh token for a new 3-legged access token. This allows access tokens to be refreshed without having the member reauthorize your application.
Parameters:
Parameter
Type
Required?
Description
refresh_token
str
Yes
The authorization code to exchange for an access token
Use client credential flow (2-legged OAuth) to retrieve a 2-legged access token for accessing APIs that are not member-specific. Developer applications do not have the client credential flow enabled by default.
All request methods of the AuthClient return a response object subclassed from BaseAuthResponse, containing standard response data, along with the original, raw response object.