-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.py
74 lines (64 loc) · 2.67 KB
/
authorization.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import urlparse
import json
import requests
from requests_oauthlib import OAuth1
from secret import CLIENT_KEY, CLIENT_SECRET
from urls import *
def get_request_token():
""" Get a token allowing us to request user authorization """
oauth = OAuth1(CLIENT_KEY, client_secret=CLIENT_SECRET)
response = requests.post(REQUEST_TOKEN_URL,
auth=oauth)
credentials = urlparse.parse_qs(response.content)
request_token = credentials.get('oauth_token')[0]
request_secret = credentials.get('oauth_token_secret')[0]
return request_token, request_secret
def get_user_authorization(request_token):
"""
Redirect the user to authorize the client, and get them to give us the
verification code.
"""
authorize_url = AUTHORIZE_URL
authorize_url = authorize_url.format(request_token=request_token)
print "Please go here and authorize: " + authorize_url
return raw_input('Please input the verifier: ')
def get_access_token(request_token, request_secret, verifier):
""""
Get a token which will allow us to make requests to the API
"""
oauth = OAuth1(CLIENT_KEY,
client_secret=CLIENT_SECRET,
resource_owner_key=request_token,
resource_owner_secret=request_secret,
verifier=verifier)
response = requests.post(ACCESS_TOKEN_URL, auth=oauth)
credentials = urlparse.parse_qs(response.content)
access_token = credentials.get('oauth_token')[0]
access_secret = credentials.get('oauth_token_secret')[0]
return access_token, access_secret
def store_credentials(access_token, access_secret):
""" Save our access credentials in a json file """
with open('access.json', 'w') as f:
json.dump({'access_token': access_token,
'access_secret': access_secret}, f)
def get_stored_credentials():
""" Try to retrieve stored access credentials from a json file """
with open('access.json', 'r') as f:
credentials = json.load(f)
return credentials['access_token'], credentials['access_secret']
def authorize():
""" A complete OAuth authentication flow """
try:
access_token, access_secret = get_stored_credentials()
except IOError:
request_token, request_secret = get_request_token()
verifier = get_user_authorization(request_token)
access_token, access_secret = get_access_token(request_token,
request_secret,
verifier)
store_credentials(access_token, access_secret)
oauth = OAuth1(CLIENT_KEY,
client_secret=CLIENT_SECRET,
resource_owner_key=access_token,
resource_owner_secret=access_secret)
return oauth