Python LinkedInA python wrapper around the LinkedIn API AuthorsÖzgür Vatansever <ozgurvt@gmail.com> Iftach Bar <iftachbar@gmail.com> License This package is licensed under the MIT License.
Thanks Thanks Pawan Deshpande for implementing the Search API.
Thanks Matthew Russell for detecting the Profile API related issues.
Thanks Horacio Durán for fixing further bugs.
Thanks Pintér Gábor for fixing encoding related bugs.
Thanks Leon van der Ree for fixing education parsing related bugs.
TODO Network API is going to be implemented ASAP.
Introductionpython-linkedin This package provides a pure python interface for the linkedin Connection, Profile,
Search, Status, Messaging and Invitation APIs.
LinkedIn (http://developer.linkedin.com) 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 simple architecture over a complicated
LinkedIn OAuth based API to make it for python programmers easy to use.
Installation You have 2 options:
Getting the code from the git repository:
git clone https://code.google.com/p/python-linkedin/
Getting the code from Google Code website of python-linkedin:
wget -c http://python-linkedin.googlecode.com/files/python-linkedin-1.8.1.tar.gz
tar zxvf python-linkedin-1.8.1.tar.gz
Enter the directory you've just downloaded, and execute the following command on shell
to install the package:
sudo python setup.py install
Usage In order to use it, you have to have an 'application key' and 'application secret'. You can create an application from here:
KEY = 'your application key'
SECRET = 'your application secret' Quick interpreter usage For quick interpreter usage, you can use the helper module
>>> from linkedin import helper
>>> api = helper.quick_api(KEY, SECRET)
Put this url in your browser and allow the permissions:
https://api.linkedin.com/uas/oauth/authorize?oauth_toekn=<some_token> Now you should paste this url in your browser and login. After you do that, the interpreter will be back and you can start using it.
>>> api.get_profile() # Or whatever method you want to call Normal usage You can use http://localhost as your RETURN URL. Return URL is the url where
LinkedIn redirects the user after he/she grants access to his/her linkedin account
>>> from linkedin import linkedin
>>> RETURN_URL = "http://localhost"
>>> api = linkedin.LinkedIn(KEY, SECRET, RETURN_URL) In OAuth terminology, there are 2 tokens that we need in order to have permission to
perform an API request. Those are request_token and access_token. Thus, this API
instance basicly intends to wrap methods of OAuth spec. which are related getting
request_token and access_token strings.
For more information; http://developer.linkedin.com/docs/DOC-1008
IMPORTANT NOTE
This library uses HMAC-SHA1 hashing algorithm to encrypt the body of any HTTP
request. Other alternatives such as 'SHA-1' or 'PLAINTEXT' are ignored.
Google App Engine
To work with google app engine, simply pass gae=True to the constructor.
>>> api = linkedin.LinkedIn(KEY, SECRET, RETURN_URL, gae=True) To get the request token, simply perform;
>>> result = api.request_token() # result can be True or False
>>> print result
>>> True
>>> api._request_token
>>> '21903a3c-b69e-4c55-a7a7-425474a84bfd'
>>> api._request_token_secret
>>> 'ca544c19-e16e-455f-b3e3-e083f1832461' If there occurs a problem while fetching the request token, you can see the error that
comes from the LinkedIn API.
>>> result = api.request_token() # result can be True or False
>>> print result
>>> False
>>> print api.get_error()
>>> 'signature_invalid' Now, this part is a bit complicated part for OAuth beginners.
If you get the request_token from LinkedIn API successfully, then open your
browser and copy the Authorization URL to grant permission to your account.
>>> api.get_authorize_url()
>>> https://api.linkedin.com/uas/oauth/authorize?oauth_token=21903a3c-b69e-4c55a7a7-425474a84bfd You can call get_authorize_url() by giving your request token as parameter explicitly.
>>> api.get_authorize_url(request_token = "21903a3c-b69e-4c55a7a7-425474a84bfd")
>>> https://api.linkedin.com/uas/oauth/authorize?oauth_token=21903a3c-b69e-4c55a7a7-425474a84bfd Copy the URL https://api.linkedin.com/uas/oauth/authorize?oauth_token=21903a3c-b69e-4c55a7a7-425474a84bfd and you will see a page like this:
Enter your email and password of your LinkedIn Account, and then click the
button 'Grant Access'.
Then you will be redirected to your RETURN_URL (http://localhost) with the following
string appended to your return URL.
http://localhost/?oauth_token=21903a3c-b69e-4c55-a7a7-425474a84bfd&oauth_verifier=17604
The appended string oauth_token=21903a3c-b69e-4c55-a7a7-425474a84bfd&oauth_verifier=17604 simply indicates that your request token is 21903a3c-b69e-4c55-a7a7-425474a84bfd and your verifier is 17604.
Then, in order to get the access token you perform access_token() method with
the verifier.
>>> result = api.access_token(verifier = "17604") # result can be True or False And Voila!
If result is True, then, now you have full access to perform any method to fetch your
LinkedIn profile or your connections, or your friends' profiles and so on.
To get your profile information, simply perform the following method;
>>> profile = api.get_profile() # profile is a Profile instance
>>> profile.first_name
>>> 'ozgur'
>>> profile.last_name
>>> 'vatansever'
>>> profile.headline
>>> 'This is my headline'
>>> profile.private_url
>>> 'http://www.linkedin.com/in/ozgurv' To get more information about your profile, perform get_profile() method with
public_url.
>>> profile = api.get_profile(url = "http://www.linkedin.com/in/ozgurv")
>>> profile.id
>>> 'COjFALsKDP'
>>> profile.location
>>> 'Turkey'
>>> profile.industry
>>> 'Computer Software'
>>> positions = profile.positions # positions is a list of Position instances
>>> [<linkedin.linkedin.Position object at 0x1a3d410>]
>>> positions[0].title
>>> 'Software Developer'
>>> positions[0].summary
>>> profile.positions[0].summary
>>> 'I work for Akinon from June 2008'
>>> profile.positions[0].company
>>> 'Akinon'
>>> profile.positions[0].start_date
>>> datetime.date(2008, 6, 1)
>>> educations = profile.educations # educations is a list of Education instances
>>> print educations
>>> [<linkedin.linkedin.Education object at 0x27fd890>]
>>> print educations[0].school_name
>>> 'Istanbul Bilgi University'
>>> print educations[0].degree
>>> 'graduate' You can fetch any user's profile if you know his/her member id or public url.
>>> profile = api.get_profile(member_id = "COjFALsKDP")
>>> print profile.id
>>> 'COjFALsKDP' You can filter the fields which you only want to get.
>>> profile = api.get_profile(member_id = "COjFALsKDP", "id", "first-name", "last-name", "current-status")
>>> print profile.id
>>> 'COjFALsKDP'
>>> print profile.first_name
>>> 'ozgur'
>>> print profile.headline
>>> None # because you didn't fetch it. For more information about profile fields, check the following url out:
http://developer.linkedin.com/docs/DOC-1061
To get your connections, simply perform get_connections() method.
For more information; take a look at http://developer.linkedin.com/docs/DOC-1004
>>> connections = api.get_connections() # connections is a list of Profile instances
>>> connections
>>> [<linkedin.linkedin.Profile object at 0x1a3d510>]
>>> connections[0].id
>>> 'js6vz2-D6x'
>>> connections[0].first_name
>>> 'test'
>>> connections[0].last_name
>>> 'user'
>>> connections[0].headline
>>> 'test user at Akinon' You can fetch your friend's connections, if you have a permission to do this.
>>> result = api.get_connections(member_id = "js6vz2-D6x") # "js6vz2-D6x" is your friend's ID.
>>> print result
>>> None
>>> print api.get_error()
>>> "Access to other member's connections denied" You can search and return profiles using get_search() by specifying a dictionary of
the GET parameters for the Search API call (see http://developer.linkedin.com/docs/DOC-1005). All keys and values should be strings.
>>> profiles = api.get_search({'company' : 'HiveFire', 'current-company' : 'true', 'start' : '1'}) You can set/clear your status performing set_status()/clear_status() methods.
If you get False as result, you can get the error performing get_error() method.
Status message should be less than 140 characters. If it is too long, it is
shortened.
For more information, you can take a look at http://developer.linkedin.com/docs/DOC-1007
>>> result = api.set_status("This is my status.")
>>> result = api.clear_status() You can send a message to yourself or your connections' inboxes by simply performing
send_message() method.
You can send your message at most 10 connections at a time. If you give more
than 10 ids, the IDs after 10th ID are ignored.
>>> result = api.send_message("This is a subject", "This is the body")
>>> print result
>>> False
>>> print api.get_error()
>>> u'Missing {mailbox-item/recipients/recipient} element' You got this error, because you didn't specify who you are sending this message to.
You can set 'send_yourself' parameter to True, so you can send this message to yourself.
>>> result = api.send_message("This is a subject", "This is the body", send_yourself = True)
>>> print result
>>> True You can give a list of member IDs to send your message to those members.
For more information, you can take a look at http://developer.linkedin.com/docs/DOC-1044
>>> result = api.send_message("This is a subject", "This is the body", ["ID1", "ID2", "ID3"])
>>> print result
>>> True You can send an invitation to your friend's email to invite him/her to join your
LinkedIn network by simply performing send_invitation() method.
As usual, you can perform get_error() method if you encountered an error.
>>> result = api.send_invitation("This is a subject", "Join to my network", "ozgurvt@gmail.com", "Ozgur", "Vatansever")
>>> print result
>>> True
>>> result = api.send_invitation("This is a subject", "Join to my network", "ozgurvt", "Ozgur", "Vatansever")
>>> print result
>>> False
>>> print api.get_error()
>>> u'Invalid argument(s): {emailAddress=invalid_email [ozgurvt]}'Throttle Limits LinkedIn API keys are throttled by default. You must take a look at
http://developer.linkedin.com/docs/DOC-1112
|