Modern Authentication - OAuth
With Microsoft moving away from "Basic/Legacy" Authentication and towards Modern Authentication using OAuth, I was wondering what the roadmap was for this library to support OAuth in the near future? Thanks! 🙂
Hello,
I would like to confirm OAuth is supported. By default The Microsoft Authentication Library (MSAL) for Python library comes as a dependency to obtain tokens to call Microsoft Graph API.
Here is an example which demonstrate how to initialize GraphClient for calling OneDrive API endpoint where token is aquired via client credentials flow:
import msal
from office365.graph_client import GraphClient
def acquire_token():
"""
Acquire token via MSAL
"""
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
app = msal.ConfidentialClientApplication(
authority=authority_url,
client_id='{client_id}',
client_credential='{client_secret}'
)
token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
return token
client = GraphClient(acquire_token)
drives = client.drives.get().execute_query()
Another OAuth spec compliant libraries such as adal are supported as well. Here is the similar example but token is acquired via acquire_token_with_client_credentials method:
Prerequisite:
adalneeds to be installed first, for instance:pip install adal
import adal
from office365.graph_client import GraphClient
def acquire_token_func():
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
auth_ctx = adal.AuthenticationContext(authority_url)
token = auth_ctx.acquire_token_with_client_credentials(
"https://graph.microsoft.com",
"{client_id}",
"{client_secret}")
return token
client = GraphClient(acquire_token_func)
drives = client.drives.get().execute_query()
@vgrem Could you share an example with sharepoint as well?
Hi thank you for this, I am however having an issue:
When I provide a method to obtain a bearer token I get an error in auth_token_provider.py line :
def _get_authorization_header(self):
return '{token_type} {access_token}'.format(token_type=self._cached_token.tokenType,
access_token=self._cached_token.accessToken)
I am providing a bearer headert :
{'token_type': 'Bearer', 'expires_in': 3599, 'ext_expires_in': 3599, 'access_token': '
The code fails to recognize this fact and fails at this point as it tries to build one.
@vgrem Could you share an example with sharepoint as well?
bump