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)

I tried to authenticate using;
credentials=ServicePrincipalCredentials(client_id=client_id,secret=client_secret,tenant=tenant_id) as well

but I got the following error:
'ServicePrincipalCredentials' object has no attribute 'get_token'. Did you mean: 'set_token'?

Could You explain what is the cause of the problem and how could it be resolved?

Thanks in advance,

Hilal
  • 41
  • 7
  • If the answer was helpful, Please [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), so that others who encounter the same issue can find this solution and fix their problem. – Ansuman Bal Feb 22 '22 at 11:21

1 Answers1

0

ServicePrincipalCredentials will give the same error as its deprecated version of Client Credential so instead of that you will need to use the ClientSecretCredential Only .

I tested the same from my environment using the below code:

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)

Versions I am using are azure-identity == 1.7.1 & azure-mgmt-keyvault == 9.3.0.

Output:

enter image description here

Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • I think pip upgrade is needed. Thanks – Hilal Feb 16 '22 at 09:13
  • 1
    I prepared similar solution, but for Azure storage modification. I have two account linked using Az Lighthouse, I tested remote Account Storage Account modification using python code (azure-identity), from my laptop it works. But when I run my code from Automation runbook, I see error message `' ClientSecretCredential ' object has no attribute 'signed_session'`. Is there any additional actions required when running Python code from Automation runbook? – tester81 Apr 24 '22 at 18:08