Skip to content

Instantly share code, notes, and snippets.

@nzbart
Created September 28, 2021 22:21
Embed
What would you like to do?
Microsoft Todo query using Microsoft Graph
from msal import PublicClientApplication
import requests
def get_authorisation_token():
app = PublicClientApplication(
"***Your Application (client) ID****",
authority="https://login.microsoftonline.com/consumers")
scopes = [
"User.Read",
"Tasks.Read",
"Tasks.Read.Shared",
"Tasks.ReadWrite",
"Tasks.ReadWrite.Shared"]
result = app.acquire_token_interactive(scopes=scopes)
if "access_token" in result:
return result["access_token"]
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id"))
raise RuntimeError("Failed to authenticate.")
def call_api(api):
r = requests.get(f'https://graph.microsoft.com/v1.0/me/{api}', headers={"Authorization": f"Bearer {token}"})
return r.json()
def get_lists():
lists = call_api("todo/lists")
return list(map(lambda l: {'name': l['displayName'], 'id': l['id']}, lists['value']))
def get_tasks(list_id):
tasks = call_api(f"todo/lists/{list_id}/tasks")
return tasks['value']
token = get_authorisation_token()
lists = get_lists()
print(lists)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment