import json
import dataclasses
import requests
@dataclasses.dataclass
class Credentials:
access_token: str
refresh_token: str
api_url: str
DATA_FILE = 'creds.json'
ENDPOINT = 'https://somtoday.nl/oauth2/token'
CLIENT_ID = 'D50E0C06-32D1-4B41-A137-A9A850C892C2'
def refresh():
body = {
"grant_type": 'refresh_token',
"refresh_token": read('refresh_token'),
"client_id": CLIENT_ID
}
data = requests.post(ENDPOINT, data=body).json()
write(data) # Save credentials for refreshing the next time.
api_url = data.get("somtoday_api_url", "https://api.somtoday.nl")
refresh_token = data.get("refresh_token")
access_token = data.get("access_token")
return Credentials(access_token, refresh_token, api_url)
def read(key = None):
with open(DATA_FILE, "r") as file:
data = json.load(file)
return data[key] if key else data
def write(data):
with open(DATA_FILE, "w") as file:
json.dump(data, file)
Python script for (re)authenticating with the Somtoday API.