-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth.py
52 lines (42 loc) · 1.37 KB
/
oauth.py
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
42
43
44
45
46
47
48
49
50
51
52
import json
import tornado.gen
from tornado.httpclient import AsyncHTTPClient
import settings
GH_URLS = {
'get_access_token': 'https://github.com/login/oauth/access_token',
'get_user_data': 'https://api.github.com/user?access_token={}'
}
class OAuth(object):
access_token = None
gh_code = None
def __init__(self, gh_code):
self.client = AsyncHTTPClient()
self.gh_code = gh_code
@tornado.gen.coroutine
def set_access_token(self):
pay_load = {
'client_id': settings.GH_OAUTH_CLIENT_ID,
'client_secret': settings.GH_OAUTH_CLIENT_SECRET,
'code': self.gh_code,
}
resp = yield self.client.fetch(
GH_URLS['get_access_token'],
method='POST',
body=json.dumps(pay_load),
headers={
'content-type': 'application/json',
'Accept': 'application/json',
}
)
self.access_token = json.loads(
resp.body.decode('utf8')
).get('access_token', None)
@tornado.gen.coroutine
def get_user_data(self):
if not self.access_token:
yield self.get_access_token()
resp = yield self.client.fetch(
GH_URLS['get_user_data'].format(self.access_token),
user_agent='GROT server'
)
return json.loads(resp.body.decode('utf8'))