Skip to content

Commit 416ecce

Browse files
committed
Merge pull request ozgur#1 from ozgur/linkedin_oauth2
LinkedIn OAuth 2.0 Implementation
2 parents d6f65bc + 448c4e6 commit 416ecce

File tree

11 files changed

+929
-1164
lines changed

11 files changed

+929
-1164
lines changed

README.md

Lines changed: 310 additions & 48 deletions
Large diffs are not rendered by default.

README.rst

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ Python LinkedIn
33

44
Python interface to the LinkedIn API
55

6-
.. image:: https://travis-ci.org/ozgur/python-linkedin.png?branch=master
7-
:target: https://travis-ci.org/ozgur/python-linkedin
86

9-
10-
This library provides a pure python interface for the LinkedIn **Connection**, **Profile**, **Search**, **Status**, **Messaging** and **Invitation** APIs.
7+
This library provides a pure Python interface to the LinkedIn **Profile**, **Group**, **Company**, **Jobs**, **Search**, **Share**, **Network** and **Invitation** REST APIs.
118

129
`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 lightweight interface over a complicated LinkedIn OAuth based API to make it for python programmers easy to use.
1310

@@ -20,9 +17,13 @@ You can install **python-linkedin** library via pip:
2017
2118
$ pip install python-linkedin
2219
23-
API Keys
24-
--------------------
25-
In order to use the LinkedIn API, you have an **application key** and **application secret**. For debugging purposes I can provide you those. You can use the following as api key and secret:
20+
Authentication
21+
-----------------------
22+
23+
LinkedIn REST API uses **Oauth 2.0** protocol for authentication. In
24+
order to use the LinkedIn API, you have an **application key** and **application secret**. You can get more detail from `here <http://developers.linkedin.com/documents/authentication>`_.
25+
26+
For debugging purposes you can use the credentials below. It belongs to my test application. Nothing's harmful.
2627

2728
.. code-block:: python
2829
@@ -32,102 +33,51 @@ In order to use the LinkedIn API, you have an **application key** and **applicat
3233
You can also get those keys from `here <http://developer.linkedin.com/rest>`_.
3334

3435

35-
Quick Usage From Python Interpreter
36-
---------------------------------------------------------
37-
38-
For testing the library using an interpreter, use the quick helper.
39-
40-
.. code-block:: python
41-
42-
from linkedin import helper
43-
api = helper.quick_api(<Your KEY>, <Your SECRET>)
44-
45-
This will print a url to the screen. Go into this URL using a browser, after you login, the method will return with an API object you can now use.
46-
47-
.. code-block:: python
48-
49-
api.get_profile()
50-
51-
Usage
52-
------------
53-
54-
You can use **http://localhost** as the return url. Return URL is a url where LinkedIn redirects the user after he/she grants access to your application.
36+
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:
5537

5638
.. code-block:: python
5739
5840
from linkedin import linkedin
5941
60-
RETURN_URL = 'http://localhost'
61-
api = linkedin.LinkedIn(<Your KEY>, <Your SECRET>, RETURN_URL)
62-
result = api.request_token()
63-
if result is True:
64-
api.get_authorize_url() # open this url on your browser
42+
API_KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
43+
API_SECRET = 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'
44+
RETURN_URL = 'http://localhost:8000'
45+
authentication = linkedin.LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, linkedin.PERMISSIONS.enums.values())
46+
print authentication.authorization_url # Open this url with your browser
47+
application = linkedin.LinkedInApplication(authentication)
6548
66-
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:
49+
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**:
6750

6851
.. code-block:: python
6952
70-
http://localhost/?oauth_token=0b27806e-feec-41d4-aac5-619ba43770f1&oauth_verifier=04874"
53+
"http://localhost:8000/?code=AQTXrv3Pe1iWS0EQvLg0NJA8ju_XuiadXACqHennhWih7iRyDSzAm5jaf3R7I8&state=ea34a04b91c72863c82878d2b8f1836c"
7154
7255
73-
This means that the **auth_verifier** value is 04874. After you get the verifier, you call the **.access_token()** method to get the access token.
56+
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.
7457

7558
.. code-block:: python
7659
77-
result = api.access_token(verifier='04874')
78-
if result is True:
79-
profile = api.get_profile()
80-
print profile.id
60+
authentication.authorization_code = 'AQTXrv3Pe1iWS0EQvLg0NJA8ju_XuiadXACqHennhWih7iRyDSzAm5jaf3R7I8'
61+
authentication.get_access_token()
8162
82-
If you know your public url, call the method above with your public url for more information.
8363
84-
.. code-block:: python
85-
86-
profile = api.get_profile(member_id=None, url='http://www.linkedin.com/in/ozgurv')
87-
print profile.id, profile.first_name, profile.last_name, profile.picture_url
88-
89-
To fetch your connections, simply call:
90-
91-
.. code-block:: python
92-
93-
connections = api.get_connections()
94-
95-
You can set/clear your status by calling **.set_status()** or **.clear_status()** methods. If you get False as the result, you can get the error by calling **.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 `DOC-1007 <http://developer.linkedin.com/docs/DOC-1007>`_
96-
97-
.. code-block:: python
98-
99-
result = api.set_status('This is my status.')
100-
result = api.clear_status()
101-
102-
You can send a message to yourself or your connections' inboxes by simply calling **.send_message()** method. You can send your message at most 10 connections at a time. If you give more than ten IDs, the IDs after 10th one are ignored. For more information, you can take a look at `DOC-1044 <http://developer.linkedin.com/docs/DOC-1044>`_.
103-
104-
.. code-block:: python
105-
106-
result = api.send_message('This is a subject', 'This is the body')
107-
if result is False:
108-
print api.get_error()
109-
u'Missing {mailbox-item/recipients/recipient} element'
64+
Quick Usage From Python Interpreter
65+
---------------------------------------------------------
11066

111-
You can set the parameter **send_yourself** to True, so you can send the message to yourself.
67+
For testing the library using an interpreter, use the quick helper.
11268

11369
.. code-block:: python
11470
115-
api.send_message('This is a subject', 'This is the body', ['ID1', 'ID2', 'ID3'], send_yourself=True)
71+
from linkedin import server
72+
application = server.quick_api(<Your KEY>, <Your SECRET>)
11673
117-
You can send an invitation to your friend's email to invite them to join your LinkedIn network by simply calling **.send_invitation()** method.
74+
This will print the authorization url to the screen. Go into this URL using a browser, after you login, the method will return with an API object you can now use.
11875

11976
.. code-block:: python
12077
121-
result = api.send_invitation('This is a subject', 'Join to my network', 'Ozgur', 'Vatansever', '[email protected]')
122-
print result
123-
True
124-
125-
result = api.send_invitation('This is a subject', 'Join to my network', 'Ozgur', 'Vatansever', 'ozgurvt')
126-
if result is False:
127-
print api.get_error()
128-
u'Invalid argument(s): {emailAddress=invalid_email [ozgurvt]}'
78+
api.get_profile()
12979
130-
Throttle Limits
131-
----------------------------
13280
133-
LinkedIn API keys are throttled by default. You should take a look at `DOC-1112 <http://developer.linkedin.com/docs/DOC-1112>`_ to get more information.
81+
More
82+
-----------------
83+
For more information, visit the `homepage <http://ozgur.github.com/python-linkedin/>`_ of the project.

examples/__init__.py

Whitespace-only changes.

examples/authentication.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from linkedin.linkedin import (LinkedInAuthentication, LinkedInApplication,
2+
PERMISSIONS)
3+
4+
5+
if __name__ == '__main__':
6+
API_KEY = 'wFNJekVpDCJtRPFX812pQsJee-gt0zO4X5XmG6wcfSOSlLocxodAXNMbl0_hw3Vl'
7+
API_SECRET = 'daJDa6_8UcnGMw1yuq9TjoO_PMKukXMo8vEMo7Qv5J-G3SPgrAV0FqFCd0TNjQyG'
8+
RETURN_URL = 'http://localhost:8000'
9+
authentication = LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL,
10+
PERMISSIONS.enums.values())
11+
print authentication.authorization_url
12+
application = LinkedInApplication(authentication)

linkedin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '1.8.1'
1+
__version__ = '2.0'
22
VERSION = tuple(map(int, __version__.split('.')))

linkedin/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class BaseLinkedInError(Exception):
2+
pass
3+
4+
5+
class LinkedInHTTPError(BaseLinkedInError):
6+
pass
7+
8+
9+
class LinkedInError(BaseLinkedInError):
10+
def __init__(self, error):
11+
if 'error' in error:
12+
Exception.__init__(self, u'%s: %s' % (error['error'],
13+
error['error_description']))
14+
else:
15+
Exception.__init__(self, u'%s: %s' % ('Request Error',
16+
error['message']))

0 commit comments

Comments
 (0)