python-3.x - 无法调用 PublicClientApplication.initiate_auth_code_flow() 函数

背景:总的来说,我是一个相对业余的程序员,所以我可能在这里吠错了树。我的 AD 组织需要 MFA,因此简单的 PublicClientApplication.acquire_token_by_username_password() 函数是不够的。

我的代码片段附在下面(我正在从 json 配置文件加载参数):

import json
import logging
import requests
import msal


# Optional logging
# logging.basicConfig(level=logging.DEBUG)  # Enable DEBUG log for entire script
# logging.getLogger("msal").setLevel(logging.INFO)  # Optionally disable MSAL DEBUG logs

with open("config.json", "r") as jsonfile:
    config = json.load(jsonfile)
    print("Read successful")
    # Create a preferably long-lived app instance which maintains a token cache.
    app = msal.PublicClientApplication(
        config["client_id"], authority=config["authority"],
        client_credential=config.get("client_secret"),
        # token_cache=...  # Default cache is in memory only.
                           # You can learn how to use SerializableTokenCache from
                           # https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache
        )
    
    
    # The pattern to acquire a token looks like this.
    result = None
    
    # Skipping error condition
    
    # Firstly, check the cache to see if this end user has signed in before
    accounts = app.get_accounts(username=config["username"])
    if accounts:
        logging.info("Account(s) exists in cache, probably with token too. Let's try.")
        result = app.acquire_token_silent(config["scope"], account=accounts[0])
    
    if not result:
        logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
        #initiate authorization code flow
        flow = app.initiate_auth_code_flow(config["scope"])
        #check if token is good
        result = app.acquire_token_by_authorization_code(flow, config["scope"])

尝试调用时:

app.initiate_auth_code_flow(config["scope"])

我收到错误:

'PublicClientApplication' object has no attribute 'initiate_auth_code_flow'

我知道initial_auth_code_flow() 肯定是msallib 的一部分,并且在PublicClientApplication 类中,所以我在这里不知所措。

回答1

我最初使用 conda 命令安装了 msal:

conda install -c conda-forge/label/cf202003 msal

使用以下 conda 命令重新安装 msal 解决了我的问题:

conda install -c conda-forge msal

相似文章

最新文章