跳转到主要内容

使用python requests和MSAL进行身份验证

项目描述

使用python requests和MSAL进行身份验证。这使用MSAL缓存重复请求。

All Contributors https://img.shields.io/badge/License-BSD%203--Clause-yellow.svg https://img.shields.io/pypi/v/msal_requests_auth.svg https://pepy.tech/badge/msal_requests_auth https://img.shields.io/conda/vn/conda-forge/msal_requests_auth.svg https://github.com/corteva/msal-requests-auth/workflows/Tests/badge.svg https://codecov.io/gh/corteva/msal-requests-auth/branch/master/graph/badge.svg https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white https://img.shields.io/badge/code%20style-black-000000.svg

错误/问题

用法

兼容性

设备代码流

  • 自版本0.2.0起:无头

  • 自版本0.6.0起:MSAL_REQUESTS_AUTH_HEADLESS环境变量

  • 自版本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,
    )

客户端凭据流

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,
)

安装

要安装msal-requests-auth,请在您的终端中运行此命令

$ python -m pip install msal_requests_auth

如果您使用conda

$ conda install -c conda-forge msal_requests_auth

Windows密钥环后端

Windows凭证存储器默认由keyring使用。然而,其密码长度限制通常会导致令牌无法存储。一个替代的后端可能可以解决这个限制。在选择后端时,请确保您了解其限制。

请考虑使用keyrings.alt作为替代的keyring后端。

python -m pip install keyrings.alt

以下是如何为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"
)

致谢

本软件包是用Cookiecutteraudreyr/cookiecutter-pypackage项目模板创建的。

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪一个,请了解更多关于安装包的信息。

源分布

msal_requests_auth-0.8.1.tar.gz (14.8 kB 查看散列)

上传时间

构建分布

msal_requests_auth-0.8.1-py3-none-any.whl (9.8 kB 查看散列)

上传时间 Python 3

支持者

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误记录 StatusPageStatusPage 状态页面