-
Notifications
You must be signed in to change notification settings - Fork 0
/
o_auth.py
59 lines (45 loc) · 2.3 KB
/
o_auth.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
import os
import logging
# Using OAuth1Session
from requests_oauthlib import OAuth1Session
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
try:
CLIENT_KEY = os.environ['OSM_KEY']
CLIENT_SECRET = os.environ['OSM_SECRET']
except KeyError:
logger.exception('please set up environment variables OSM_KEY and OSM_SECRET')
raise AttributeError
class Authorisation:
def __init__(self, req_token_url=None, base_auth_url=None, acc_token_url=None):
self.oauth = OAuth1Session(CLIENT_KEY, client_secret=CLIENT_SECRET)
self.owner_key = ''
self.owner_secret = ''
self.verifier = ''
self.request_token_url = req_token_url or 'https://master.apis.dev.openstreetmap.org/oauth/request_token'
self.base_authorization_url = base_auth_url or 'https://master.apis.dev.openstreetmap.org/oauth/authorize'
self.access_token_url = acc_token_url or 'https://master.apis.dev.openstreetmap.org/oauth/access_token'
# Using OAuth1Session
def request_token(self):
fetch_response = self.oauth.fetch_request_token(self.request_token_url)
logger.debug(fetch_response)
self.owner_key = fetch_response.get('oauth_token')
self.owner_secret = fetch_response.get('oauth_token_secret')
return self.owner_key, self.owner_secret
def prepare_auth_url(self):
authorization_url = self.oauth.authorization_url(self.base_authorization_url)
print('Please go here and authorize,', authorization_url) # TODO send this message to user
# Using OAuth1Session
def authorize(self, response):
oauth_response = self.oauth.parse_authorization_response(response)
logger.debug(oauth_response)
self.verifier = oauth_response.get('oauth_verifier')
return self.verifier
# Using OAuth1Session
def access_token(self):
self.oauth = OAuth1Session(CLIENT_KEY,
client_secret=CLIENT_SECRET,
resource_owner_key=self.owner_key,
resource_owner_secret=self.owner_secret,
verifier=self.verifier)
oauth_tokens = self.oauth.fetch_access_token(self.access_token_url)