Download your LinkedIn contacts with Python

Hello everybody!

This post will be about two new things: Using Python for getting Social Data and working with the LinkedIn API and is based on Matthew A. Russell´s book “Mining the Social Web“. If you are interested in that topic you should really take a look at this book.

The main feature of this API is getting contacts and this is what I will do in this tutorial. In the following tutorials I will show you then how to visualize you contacts. But for now we download our contacts.


First of all we have to get an API key. You can get it at

linkedin pythonYou have to login into your account and then on “Add New Application”. linkedin python

There you have to give your new app a name and fill in some other details. For our purpose, as we don´t want to publish the app, you can write whatever you want. It is just important  “r_basicprofile” in the section OAuth User Agreement. linkedin python

Then just click on “Add Application” and you can see your app keys.


Ok now we have our keys and can start with the Python part. You can use whatever Python IDE you want. I would recomment pyCharm as it works really great for me.

For downloading the contacts we use the linkedin-python package and for displaying them we use the prettytable package. Just open a terminal and type in:

pip install python-linkedin
pip install prettytable

Let´s take a look at the python code.
First of all we have to import the needed packages, set our LinkedIn app keys create create a so called “app” in the python code. This app will be our connection to the LinkedIn API.


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

app = linkedin.LinkedInApplication(auth)

With the help of this app we can get our contacts with the get_connections() function. Then we store them in a JSON object.

import json
connections = app.get_connections()

linkedin_contacts = 'contacts.json'

f = open(linkedin_contacts, 'w')
f.write(json.dumps(connections, indent=1))

Then we can use the prettytable packe and print them as a nice looking table:

from prettytable import PrettyTable
pt = PrettyTable(field_names=['Name', 'Location'])
pt.align = 'l'
[pt.add_row((c['firstName'] + ' ' + c['lastName'], c['location']['name']))
for c in connections['values']
if c.has_key('location')]
print pt

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s