msal-requests-auth
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
New in version 0.7.0: KeyringTokenCache
import requests
import msal
from msal_requests_auth.auth import DeviceCodeAuth
from msal_requests_auth.cache import KeyringTokenCache
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 KeyringTokenCache() 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
Windows keyring backend
The Windows Credential Locker is used by default by keyring. However, its password length limitations often prevent storing tokens. An alternative backend may resolve this limitation. When choosing a backend, be sure you are aware of its limitations.
keyrings.alt is an alternative keyring backend to consider:
python -m pip install keyrings.alt
Here is an example of how to set an alternative backend for keyring:
import keyring
keyring.core._config_path().parent.mkdir(parents=True, exist_ok=True)
keyring.core._config_path().write_text(
"[backend]\ndefault-keyring=keyrings.alt.Windows.EncryptedKeyring"
)
Credits
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
Metadata
Release files for msal-requests-auth 0.9.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| msal_requests_auth-0.9.3.tar.gz | 15.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| msal_requests_auth-0.9.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.3 kB
Release files / msal_requests_auth-0.9.3.tar.gz
| Download URL | msal_requests_auth-0.9.3.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b611309117de6846febfa16df42f39a31b474251fc162fdc3fc77c665c0d1b2
|
|
BLAKE2b-256 checksum How to use checksums |
4e75fc8de37ef8a05d3e8203ac6945f8f61ae990b4e8550158897003456ba31e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 15, 2026.
Transparency logRelease files / msal_requests_auth-0.9.3-py3-none-any.whl
| Download URL | msal_requests_auth-0.9.3-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
27441671558a0bbd16c447f80cf44f106871a544e57c4117b6835120f86c8c08
|
|
BLAKE2b-256 checksum How to use checksums |
7935f9eebe1ce5ac0fb9ee59397a19de6b6f95a675a8411621c8d4650793b3ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 15, 2026.
Transparency log