Created
September 28, 2021 22:21
-
-
Save nzbart/9cb394ec7e79d752719a86fb4b05cae5 to your computer and use it in GitHub Desktop.
Microsoft Todo query using Microsoft Graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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