微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

没有找到环境配置 DefaultAzureCredential()

如何解决没有找到环境配置 DefaultAzureCredential()

我正在尝试使用此 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。理想情况下,我想使用环境变量或配置文件来管理此配置。有没有办法做到这一点?

解决方法

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。