Authentication using python requests and MSAL
Project description
Authentication using python requests and MSAL. This uses the MSAL cache for repeated requests.
Bugs/Questions
Report bugs/feature requests: https://github.com/corteva/msal-requests-auth/issues
Ask questions: https://github.com/corteva/msal-requests-auth/discussions
Usage
Compatible with:
Device Code Flow
New in version 0.2.0: headless
New in version 0.6.0: MSAL_REQUESTS_AUTH_HEADLESS environment variable
import requests
import msal
from msal_requests_auth.auth import DeviceCodeAuth
from msal_requests_auth.cache import SimpleTokenCache
client_id = "<client ID from Azure AD>"
tenant_id = "<tenant ID from Azure AD>"
application_id = "<client ID of application you want to get a token for from Azure AD>"
with SimpleTokenCache() as token_cache:
app = msal.PublicClientApplication(
client_id,
authority=f"https://login.microsoftonline.com/{tenant_id}/",
token_cache=token_cache,
)
auth = DeviceCodeAuth(
client=app,
scopes=[f"{application_id}/.default"],
)
response = requests.get(
endpoint,
auth=auth,
)
Client Credentials Flow
import requests
import msal
from msal_requests_auth.auth import ClientCredentialAuth
client_id = "<client ID from Azure AD>"
client_secret = "<client secret for client in Azure AD>"
tenant_id = "<tenant ID from Azure AD>"
application_id = "<client ID of application you want to get a token for from Azure AD>"
app = msal.ConfidentialClientApplication(
client_id,
authority=(f"https://login.microsoftonline.com/{tenant_id}/"),
client_credential=client_secret,
)
auth = ClientCredentialAuth(
client=app,
scopes=[f"{application_id}/.default"],
)
response = requests.get(
endpoint,
auth=auth,
)
Installation
To install msal-requests-auth, run this command in your terminal:
$ python -m pip install msal_requests_auth
If you use conda:
$ conda install -c conda-forge msal_requests_auth
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
msal_requests_auth-0.6.1.tar.gz
(12.8 kB
view hashes)
Built Distribution
Close
Hashes for msal_requests_auth-0.6.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e38b644510a03a53a767388ffd17c2f97e6c16d0d7dd52c94c9dfd2c9bd2bc21 |
|
MD5 | 05fe6d1f6a20fc79b606a158cd49c213 |
|
BLAKE2b-256 | 1e90ace6d7a57da223462d4e1283369022fc6aad40f969cc98ed5de91f0d92ac |