没有找到环境配置。 DefaultAzureCredential()

时间:2021-03-17 19:59:26

标签: azure-sdk-python

我正在尝试使用此 Python 示例对具有 Azure 服务的客户端进行身份验证

# pip install azure-identity
from azure.identity import DefaultAzureCredential
# pip install azure-mgmt-compute
from azure.mgmt.compute import ComputeManagementClient
# pip install azure-mgmt-network
from azure.mgmt.network import NetworkManagementClient
# pip install azure-mgmt-resource
from azure.mgmt.resource import ResourceManagementClient

    SUBSCRIPTION_ID = creds_obj['SUBSCRIPTION_ID']

    # Create client
    # For other authentication approaches, please see: https://pypi.org/project/azure-identity/
    resource_client = ResourceManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    network_client = NetworkManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )
    compute_client = ComputeManagementClient(
        credential=DefaultAzureCredential(),
        subscription_id=SUBSCRIPTION_ID
    )

我不断收到No environment configuration found. 代码示例直接来自微软 github:https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/resources/azure-mgmt-resource/azure/mgmt/resource/resources/_resource_management_client.py。理想情况下,我想使用环境变量或配置文件来管理此配置。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

Azure Identity 客户端库用于 Python 时,DefaultAzureCredential 尝试按以下顺序通过以下机制进行身份验证,成功后停止:

enter image description here

您可以设置 Environment Variables 来修复它。

enter image description here

from azure.identity import DefaultAzureCredential

credential=DefaultAzureCredential()

config 中设置属性并使用 ClientSecretCredential 创建凭据。

from azure.identity import ClientSecretCredential

subscription_id = creds_obj["AZURE_SUBSCRIPTION_ID"]
tenant_id = creds_obj["AZURE_TENANT_ID"]
client_id = creds_obj["AZURE_CLIENT_ID"]
client_secret = creds_obj["AZURE_CLIENT_SECRET"]

credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)