-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathosm_access.py
37 lines (29 loc) · 1.52 KB
/
osm_access.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
"""This script is used to get osm authentication token and secret token."""
from typing import Tuple, Optional
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import OAuth2Token
OSM_AUTH_URL = 'https://www.openstreetmap.org/oauth2/authorize'
OSM_TOKEN_URL = 'https://www.openstreetmap.org/oauth2/token'
# Credentials dedicated for GitHub community
CLIENT_ID = 'xCDlXoN-gVeXsXMHu8N5VArN4iWBDwuMgZAlf5PlC7c'
CLIENT_SECRET = "UICSTaxQkQsSl-osmcbqd5CXJIak5fvw9BF_F152BeE"
def __osm_auth_service() -> OAuth2Session:
"""Factory method that builds osm auth service"""
oauth = OAuth2Session(client_id=CLIENT_ID,
redirect_uri="urn:ietf:wg:oauth:2.0:oob",
scope=["openid", "read_prefs"])
return oauth
def osm_auth(request_user_action, error_handle) -> Tuple[Optional[str], Optional[str]]:
service = __osm_auth_service()
authorization_url, _ = service.authorization_url(OSM_AUTH_URL)
response = request_user_action(authorization_url)
# pylint: disable=W0703
try:
access_token_response: OAuth2Token = service.fetch_token(OSM_TOKEN_URL,
code=response,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
return access_token_response.get("access_token"), CLIENT_SECRET
except Exception as ex:
error_handle(ex)
return None, None