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, AuthorizationHandler, AuthMiddlewareOptions, TokenCredentialAuthProvider
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/
Create an authorization provider object and a list of scopes
scopes = ['mail.send', 'user.read']
auth_provider = TokenCredentialAuthProvider(scopes, device_credential)
graph_session = GraphSession(auth_provider)
result = graph_session.get('/me')
print(result.json())