因此,我在尝试验证Python环境时遇到了各种各样的问题。
我在这里的最终目标是使用Azure AppConfiguration SDK从Azure中的AppConfig服务读取值。但现在我在第一个栏上就失败了。
仅供参考,我正在使用VS代码(以管理员身份运行)在Windows 11计算机上运行所有这些。我安装了最新的Azure CLI。
我的
Azure应用程序配置
脚本如下:
from azure.identity import DefaultAzureCredential
from azure.appconfiguration import AzureAppConfigurationClient
appCfgUrl = os.environ["AppConfigUrl"]
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(appCfgUrl, credential)
value = client.get_configuration_setting(configKey)
但这会导致错误:
DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
EnvironmentCredential: Authentication failed: AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope https://Endpoint=https://foo.azconfig.io;Id=XXXXXX;Secret=YYYYYY=/.default is not valid.
所以我认为AppConfig SDK或我实现它的方式可能有问题,所以我想我应该尝试直接连接到
Azure CLI
根据建议直接使用以下代码
Authenticating Azure CLI with Python SDK
import os
from azure.cli.core import get_default_cli
az_cli = get_default_cli()
clientId = os.environ["AZURE_CLIENT_ID"]
clientSecret = os.environ["AZURE_CLIENT_SECRET"]
tenantId = os.environ["AZURE_TENANT_ID"]
az_cli.invoke(['login', '--service-principal',
'-u', clientId,
'-p', clientSecret,
'--tenant',tenantId])
然而,这只会导致错误消息
No module named 'azure.cli.command_modules'.
'login' is misspelled or not recognized by the system.
我都试过了
pip安装azure.cli.core
以及
pip安装azure cli
我还升级了我机器上的Azure CLI。我可以在Bash和PowerShell中成功地向Azure CLI进行身份验证,而不会出现任何问题,而且我还有另一个问题。NET C#项目,该项目使用Azure CLI没有问题。
在这一点上,我真的很伤心。我尝试过为服务主体使用硬编码的值,以及使用本地定义的环境变量,但都不起作用。
如前所述,我在中完成了所有这些。NET在单元测试、Azure功能应用程序和Azure Web应用程序中都没有任何问题,但在Python中这样做似乎刚刚遇到了困难。