无法调用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'

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

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

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

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

conda install -c conda-forge msal

最新更新