forked from ozgur/python-linkedin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
41 lines (34 loc) · 1.54 KB
/
Copy pathserver.py
File metadata and controls
41 lines (34 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
from __future__ import print_function
import future.moves.http.server as http_server
import future.moves.urllib.parse as urllib_parse
from .linkedin import LinkedInApplication, LinkedInAuthentication, PERMISSIONS
def quick_api(api_key, secret_key):
"""
This method helps you get access to linkedin api quickly when using it
from the interpreter.
Notice that this method creates http server and wait for a request, so it
shouldn't be used in real production code - it's just an helper for debugging
The usage is basically:
api = quick_api(KEY, SECRET)
After you do that, it will print a URL to the screen which you must go in
and allow the access, after you do that, the method will return with the api
object.
"""
auth = LinkedInAuthentication(api_key, secret_key, 'http://localhost:8000/',
list(PERMISSIONS.enums.values()))
app = LinkedInApplication(authentication=auth)
print(auth.authorization_url)
_wait_for_user_to_enter_browser(app)
return app
def _wait_for_user_to_enter_browser(app):
class MyHandler(http_server.BaseHTTPRequestHandler):
def do_GET(self):
p = self.path.split('?')
if len(p) > 1:
params = urllib_parse.parse_qs(p[1], True, True)
app.authentication.authorization_code = params['code'][0]
app.authentication.get_access_token()
server_address = ('', 8000)
httpd = http_server.HTTPServer(server_address, MyHandler)
httpd.handle_request()