-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_authentication.py
40 lines (28 loc) · 1003 Bytes
/
get_authentication.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
import logging
import requests
import config as cfg
config = cfg.get_config()
creds_name = config["creds"]["name"]
creds_pw = config["creds"]["password"]
url_core_data = config["urls"]["core_data_api"]
url_core_manager = config["urls"]["core_manager_api"]
logger = logging.getLogger(__name__)
def get_auth():
"""
POST request to users/login, returns auth token
"""
try:
url_user_login = f"https://{url_core_data}/users/login"
json_payload = {"username": creds_name, "password": creds_pw}
headers = {"Accept": "application/json"}
response = requests.post(
url_user_login, headers=headers, json=json_payload, verify=False
)
response.raise_for_status()
token = response.json()["token"]
return token
except requests.exceptions.RequestException as e:
auth_err_msg = f"Error authenticating with the DIVA API: \n{e}"
logger.error(auth_err_msg)
if __name__ == "__main__":
get_auth()