0
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
tenant_id = os.environ["AZURE_TENANT_ID"]
client_id = os.environ["AZURE_CLIENT_ID"]
client_secret = os.environ["AZURE_CLIENT_SECRET"]


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

kv_client = KeyVaultManagementClient(credentials, subscription_id)

我尝试使用身份验证;
credentials=ServicePrincipalCredentials(client_id=client_id,secret=client_secret,tenant=tenant_id) 以及

但我收到以下错误:
“ServicePrincipalCredentials”对象没有属性“get_token”。你的意思是:'set_token'?

您能否解释一下问题的原因以及如何解决?

提前致谢,

4

1 回答 1

0

ServicePrincipalCredentials将给出与其已弃用的客户端凭据版本相同的错误,因此您需要使用ClientSecretCredentialOnly 。

我使用以下代码在我的环境中进行了相同的测试:

AZURE_TENANT_ID = 'Tenant_Id'
AZURE_CLIENT_ID = 'App_Id'
AZURE_CLIENT_SECRET = '<Client_Secret>'
AZURE_SUBSCRIPTION_ID = '<Subscription_Id>'
from azure.identity import ClientSecretCredential
from azure.mgmt.keyvault import KeyVaultManagementClient
credentials = ClientSecretCredential(tenant_id=AZURE_TENANT_ID, client_id=AZURE_CLIENT_ID, client_secret=AZURE_CLIENT_SECRET)

kv_client = KeyVaultManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
kv_list= kv_client.vaults.list()
for item in kv_list:
    print(item.name)

我使用的版本是azure-identity == 1.7.1& azure-mgmt-keyvault == 9.3.0

输出:

在此处输入图像描述

于 2022-02-15T09:55:35.103 回答