Skip to content

Commit 64c8d3e

Browse files
committed
Including http API example
1 parent 843b23b commit 64c8d3e

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ This library provides a pure Python interface to the LinkedIn **Profile**, **Gro
1515
You can install **python-linkedin** library via pip:
1616

1717
$ pip install python-linkedin
18-
$ pip install requests
19-
$ pip install requests_oauthlib
20-
2118

2219
## Authentication
2320

24-
The LinkedIn REST API now supports the **Oauth 2.0** protocol for authentication. This package provides a full OAuth 2.0 implementation for connecting to LinkedIn as well as an option for using an OAuth 1.0a flow that can be helpful for development purposes or just accessing your own data.
21+
The LinkedIn REST API now supports the **OAuth 2.0** protocol for authentication. This package provides a full OAuth 2.0 implementation for connecting to LinkedIn as well as an option for using an OAuth 1.0a flow that can be helpful for development purposes or just accessing your own data.
22+
23+
### HTTP API example
24+
25+
Set `LINKEDIN_API_KEY` and `LINKEDIN_API_SECRET`, configure your app to redirect to `http://localhost:8080/code`, then execute:
26+
27+
0. `http_api.py`
28+
1. Visit `http://localhost:8080` in your browser, curl or similar
29+
2. A tab in your browser will open up, give LinkedIn permission there
30+
3. You'll then be presented with a list of available routes, hit any, e.g.:
31+
4. `curl -XGET http://localhost:8080/get_profile`
2532

2633
### Developer Authentication
2734

examples/http_api.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
__author__ = 'Samuel Marks <samuelmarks@gmail.com>'
2+
__version__ = '0.1.0'
3+
4+
from SocketServer import ThreadingTCPServer
5+
from SimpleHTTPServer import SimpleHTTPRequestHandler
6+
from webbrowser import open_new_tab
7+
from json import dumps
8+
from urlparse import urlparse
9+
from os import environ
10+
from types import NoneType
11+
12+
from linkedin.linkedin import LinkedInAuthentication, LinkedInApplication, PERMISSIONS
13+
14+
PORT = 8080
15+
16+
17+
class LinkedInWrapper(object):
18+
""" Simple namespacing """
19+
API_KEY = environ.get('LINKEDIN_API_KEY')
20+
API_SECRET = environ.get('LINKEDIN_API_SECRET')
21+
RETURN_URL = 'http://localhost:{0}/code'.format(globals()['PORT'])
22+
authentication = LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, PERMISSIONS.enums.values())
23+
application = LinkedInApplication(authentication)
24+
25+
26+
liw = LinkedInWrapper()
27+
run_already = False
28+
params_to_d = lambda params: {
29+
l[0]: l[1] for l in map(lambda j: j.split('='), urlparse(params).query.split('&'))
30+
}
31+
32+
33+
class CustomHandler(SimpleHTTPRequestHandler):
34+
def json_headers(self, status_code=200):
35+
self.send_response(status_code)
36+
self.send_header('Content-type', 'application/json')
37+
self.end_headers()
38+
39+
def do_GET(self):
40+
parsedurl = urlparse(self.path)
41+
authed = type(liw.authentication.token) is not NoneType
42+
43+
if parsedurl.path == '/code':
44+
self.json_headers()
45+
46+
liw.authentication.authorization_code = params_to_d(self.path).get('code')
47+
self.wfile.write(dumps({'access_token': liw.authentication.get_access_token(),
48+
'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
49+
elif parsedurl.path == '/routes':
50+
self.json_headers()
51+
52+
self.wfile.write(dumps({'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
53+
elif not authed:
54+
self.json_headers()
55+
56+
if not globals()['run_already']:
57+
open_new_tab(liw.authentication.authorization_url)
58+
globals()['run_already'] = True
59+
self.wfile.write(dumps({'path': self.path, 'authed': type(liw.authentication.token) is NoneType}))
60+
elif authed and len(parsedurl.path) and parsedurl.path[1:] in dir(liw.application):
61+
self.json_headers()
62+
self.wfile.write(dumps(getattr(liw.application, parsedurl.path[1:])()))
63+
else:
64+
self.json_headers(412)
65+
self.wfile.write(dumps({'error': 'NotImplemented'}))
66+
67+
68+
if __name__ == '__main__':
69+
httpd = ThreadingTCPServer(('localhost', PORT), CustomHandler)
70+
71+
print 'Server started on port:', PORT
72+
httpd.serve_forever()

linkedin/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '4.1'
1+
__version__ = '4.2'
22
VERSION = tuple(map(int, __version__.split('.')))

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
requests_oauthlib

0 commit comments

Comments
 (0)