The Microsoft Graph Python client library is a lightweight wrapper around the Microsoft Graph API.
Install packages
pip install -i https://test.pypi.org/simple/ msgraphcore
pip install azure-identity
Import modules
from azure.identity import UsernamePasswordCredential, DeviceCodeCredential
from msgraphcore import GraphSession
Configure Credential Object
# Added UsernamePassword for demo purposes only, please don't use this in production.
# ugly_credential = UsernamePasswordCredential('set-clientId', 'set-username', 'set-password')
device_credential = DeviceCodeCredential(
'set-clientId')
# There are many other options for getting an access token. See the following for more information.
# https://pypi.org/project/azure-identity/
Pass the credential object and scopes to the GraphSession constructor.
scopes = ['mail.send', 'user.read']
graph_session = GraphSession(device_credential, scopes)
result = graph_session.get('/me')
print(result.json())