My enthusiasm for LinkedIn increased dramatically once I learned that LinkedIn had opened up its API to the public at large.  What is still unclear to me is how much the API allows one to get data in and out LinkedIn.  One of the best ways to find out:  dive in and see what we can learn.
In this post, I describe some first steps you can take to learn to access the LinkedIn API  with Python (a favorite programming language of mine and many):
- Get oriented by looking the main page for the API, which in this case, is  LinkedIn Developer Network.
- You'll need a set of developer keys for each application, which you can get by registering an application. Â You'll be asked to login with your LinkedIn user account email/password. Â If you don't yet have a LinkedIn user account, sign up for one. (Using the API doesn't require a separate developer account.)
- I found a few tricky bits in the registration process.  First of all, you're going to have remind yourself (or teach yourself for the first time) the basics of OAuth, an open protocol used by LinkedIn (and other websites) to authorize users.  (I won't attempt to provide such a tutorial here. A set of slides from LinkedIn does a pretty good job of giving an overview of OAuth. ) The second tricky part was that I forgot whether OAuth could support desktop applications.  It turns out that you do get 3 options for the types of app you are registering: desktop, web, and mobile.  In my case, I registered that I was creating a desktop app.
- When you are finished registering your application, you will get two important parameters for your app: Â the OAuth (consumer) key and secret. You will need these two parameters for your Python application.
- You can choose to work with the API directly at the HTTP level protocol level or look for API libraries that wrap the protocol.  I searched for such a Python library, and found pylinkedin (a barebone but functional library), whose source you can get via mercurial by
hg clone https://pylinkedin.googlecode.com/hg/ pylinkedin
- Install the library using the usual
python setup.py install
.  pylinkedin was tested on Python 2.5 but so far, I've found it to work on Python 2.6. Note that the library requires that you have the  oauth Python library installed (available via svn at Âhttp://oauth.googlecode.com/svn/code/python/
- You will need to correct a small bug that I found in
pylinkedin
(around the parsing of companies) — you'll need to edit the__init__.py
file according to instructions I posted.
Now we're ready to run the following code, which will display the first and last name of your LinkedIn "connections" (the people in your immediate circle). Â Remember that key and secret you got when you registered your application: plug them into the following program. Â (Note that a browser window should open prompting you for your LinkedIn password. Â You'll then have to enter a code that LinkedIn gives you to let the program access your LinkedIn data.)
# insert your application KEY and SECRET API_KEY = "_FILL_IN_YOUR_KEY_" SECRET_KEY = "_FILL_IN_YOUR_SECRET_" import webbrowser from linkedin import LinkedIn li = LinkedIn(API_KEY, SECRET_KEY) token = li.getRequestToken(None) # prompt user in the web browser to login to LinkedIn and then enter a code that LinkedIn gives to the user auth_url = li.getAuthorizeUrl(token) webbrowser.open(auth_url) validator = input("Enter token: ") access_token=li.getAccessToken(token,validator) # list all connections connections = li.connections_api.getMyConnections(access_token) print "number of connections: ", len(connections) for c in connections: print c.firstname, " ", c.lastname
There are obviously improvements to make in this starter code — but this should get us all on our way.
Acknowledgements: Thanks to LinkedIn for the API and to Max Lynch for pylinkedin, which made getting into the API much easier!
Post a Comment