-
Notifications
You must be signed in to change notification settings - Fork 0
/
outh_shanbay.py
53 lines (39 loc) · 1.37 KB
/
outh_shanbay.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from oauthlib.oauth2 import (
FatalClientError, OAuth2Error, TokenExpiredError, MobileApplicationClient
)
from requests_oauthlib import OAuth2Session, TokenUpdated
client_id = '42e61b1dfa3df66c783e'
client_secret = '68d90e5fe8e1af9901f0eae6d4815beeb8a9bd7a'
redirect_uri = 'https://api.shanbay.com/oauth2/auth/success/'
authorization_base_url = 'https://api.shanbay.com/oauth2/authorize/'
token_url = 'https://api.shanbay.com/oauth2/token/'
token_file = 'token.json'
def userinfo(api):
r = api.get('https://api.shanbay.com/account/')
print r.json()
def get_token():
print client_id
client = MobileApplicationClient(client_id)
print client
api = OAuth2Session(client=client, redirect_uri=redirect_uri)
authorization_url, state = api.authorization_url(authorization_base_url)
print authorization_url
print 'Please go here and authorize,', authorization_url
redirect_response = raw_input('Paste the full redirect URL here:')
token = api.token_from_fragment(redirect_response)
with open(token_file, 'w') as f:
f.write(json.dumps(token, indent=2))
return api
try:
with open(token_file) as f:
token = json.loads(f.read())
api = OAuth2Session(client_id, token=token)
userinfo(api)
except (
TokenExpiredError, TokenUpdated, OAuth2Error, FatalClientError, ValueError
) as e:
api = get_token()
userinfo(api)