Files
propedal-planner/calendar_interface.py
2022-06-12 10:39:42 +02:00

83 lines
3.4 KiB
Python

import json
from lib2to3.pgen2.tokenize import TokenError
import logging
import requests
import msal
config = {
"authority": "https://login.microsoftonline.com/propedal.at",
"client_id": "52f192c4-875d-44a2-b28a-575e920225e5", # client public id (from azure web interface)#"da3fc28c-5fcf-4884-9477-903a4420cc3d",
"scope": ["https://graph.microsoft.com/.default"], # scopes from api
"secret": "irj8Q~PliZzSe7JnXEaiWKQ6v0CAg1DTZOO~Ccsf" # api secret key (from azure web interface)#"bqP8Q~SEp_AmYYDZoEykXxZdADoCOhzOOhbO3c3T"
}
USER_ID = "simone.profus@propedal.at" # user with calendar #"2af02ca1-77fc-46fd-90af-c754306081cb" #
CALENDAR_ID = "AAMkADY0MDg1MTVjLTg5ZjItNGQxYS04MGQ3LWY2NjJmYjM0YmZhOQBGAAAAAADXD7SdVoWYQI4RYXbBumMEBwAf_ngZxs71RonY3GuLL8TVAAAAAAEGAAAf_ngZxs71RonY3GuLL8TVAADHFxN2AAA=" # calendar id - determined by /users/id/calendars
# Optional logging
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-4s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
level=logging.INFO)
# logging.getLogger("msal").setLevel(logging.INFO) # Optionally disable MSAL DEBUG logs
def get_access_token():
#with open("auth_config.json") as f:
# config = json.load(f)
# Create a preferably long-lived app instance which maintains a token cache.
app = msal.ConfidentialClientApplication(
config["client_id"], authority=config["authority"],
client_credential=config["secret"],
# token_cache=... # Default cache is in memory only.
# You can learn how to use SerializableTokenCache from
# https:#msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache
)
# The pattern to acquire a token looks like this.
result = None
# Firstly, looks up a token from cache
# Since we are looking for token for the current app, NOT for an end user,
# notice we give account parameter as None. # TODO: token never exists in cache; make app long living
result = app.acquire_token_silent(config["scope"], account=None)
if result is None:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
return app.acquire_token_for_client(scopes=config["scope"])
else:
logging.info("Token was found in cache.")
if not "access_token" in result: # a final check
logging.error(result.get("error"))
logging.error(result.get("error_description"))
logging.error(result.get("correlation_id")) # You may need this when reporting a bug
raise TokenError()
return result
def execute_request(token: dict, endpoint:str):
return requests.get( # Use token to call downstream service
endpoint,
headers={'Authorization': 'Bearer ' + token['access_token']},).json()
def execute_user_request(token, endpoint, user_id=USER_ID):
return execute_request(token, "https://graph.microsoft.com/v1.0/users/" + user_id + f"/{endpoint}")
if __name__ == "__main__":
# Calling graph using the access token
token = get_access_token()
calendars = execute_user_request(token, "calendars")
cal_name_id = [(c["name"], c["id"]) for c in calendars["value"]]
print("Available calendars are:")
print(json.dumps(cal_name_id, indent=2))
print("Events in selected calendar are:")
events = execute_user_request(token, f"calendars/{CALENDAR_ID}/events").get("value")
print(json.dumps(events, indent=2))