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
Device Code Flow
import atexit
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>"
token_cache = SimpleTokenCache()
atexit.register(token_cache.write_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.4.0.tar.gz
(10.8 kB
view details)
File details
Details for the file msal_requests_auth-0.4.0.tar.gz
.
File metadata
- Download URL: msal_requests_auth-0.4.0.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78bb50e59605a2b26cef6007400481da186c0401de9b4ee2d4e8fe58f43de78c |
|
MD5 | 8ddbc212d0e671f519d3ca94b08baabc |
|
BLAKE2b-256 | 481b7e783c24661916ddcb569cf516fa9b0e35ec19d1b1f25d3312dc5239e642 |