Skip to content

Commit

Permalink
Including http API example
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Dec 12, 2014
1 parent 843b23b commit 64c8d3e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ This library provides a pure Python interface to the LinkedIn **Profile**, **Gro
You can install **python-linkedin** library via pip:

$ pip install python-linkedin
$ pip install requests
$ pip install requests_oauthlib


## Authentication

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.
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.

### HTTP API example

Set `LINKEDIN_API_KEY` and `LINKEDIN_API_SECRET`, configure your app to redirect to `http://localhost:8080/code`, then execute:

0. `http_api.py`
1. Visit `http://localhost:8080` in your browser, curl or similar
2. A tab in your browser will open up, give LinkedIn permission there
3. You'll then be presented with a list of available routes, hit any, e.g.:
4. `curl -XGET http://localhost:8080/get_profile`

### Developer Authentication

Expand Down
72 changes: 72 additions & 0 deletions examples/http_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
__author__ = 'Samuel Marks <[email protected]>'
__version__ = '0.1.0'

from SocketServer import ThreadingTCPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from webbrowser import open_new_tab
from json import dumps
from urlparse import urlparse
from os import environ
from types import NoneType

from linkedin.linkedin import LinkedInAuthentication, LinkedInApplication, PERMISSIONS

PORT = 8080


class LinkedInWrapper(object):
""" Simple namespacing """
API_KEY = environ.get('LINKEDIN_API_KEY')
API_SECRET = environ.get('LINKEDIN_API_SECRET')
RETURN_URL = 'http://localhost:{0}/code'.format(globals()['PORT'])
authentication = LinkedInAuthentication(API_KEY, API_SECRET, RETURN_URL, PERMISSIONS.enums.values())
application = LinkedInApplication(authentication)


liw = LinkedInWrapper()
run_already = False
params_to_d = lambda params: {
l[0]: l[1] for l in map(lambda j: j.split('='), urlparse(params).query.split('&'))
}


class CustomHandler(SimpleHTTPRequestHandler):
def json_headers(self, status_code=200):
self.send_response(status_code)
self.send_header('Content-type', 'application/json')
self.end_headers()

def do_GET(self):
parsedurl = urlparse(self.path)
authed = type(liw.authentication.token) is not NoneType

if parsedurl.path == '/code':
self.json_headers()

liw.authentication.authorization_code = params_to_d(self.path).get('code')
self.wfile.write(dumps({'access_token': liw.authentication.get_access_token(),
'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
elif parsedurl.path == '/routes':
self.json_headers()

self.wfile.write(dumps({'routes': filter(lambda d: not d.startswith('_'), dir(liw.application))}))
elif not authed:
self.json_headers()

if not globals()['run_already']:
open_new_tab(liw.authentication.authorization_url)
globals()['run_already'] = True
self.wfile.write(dumps({'path': self.path, 'authed': type(liw.authentication.token) is NoneType}))
elif authed and len(parsedurl.path) and parsedurl.path[1:] in dir(liw.application):
self.json_headers()
self.wfile.write(dumps(getattr(liw.application, parsedurl.path[1:])()))
else:
self.json_headers(412)
self.wfile.write(dumps({'error': 'NotImplemented'}))


if __name__ == '__main__':
httpd = ThreadingTCPServer(('localhost', PORT), CustomHandler)

print 'Server started on port:', PORT
httpd.serve_forever()
2 changes: 1 addition & 1 deletion linkedin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '4.1'
__version__ = '4.2'
VERSION = tuple(map(int, __version__.split('.')))
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
requests_oauthlib

0 comments on commit 64c8d3e

Please sign in to comment.