Sadhna Pandey
Sadhna Pandey

Reputation: 11

Login to python script using service principal

I have registered one App in AAD, I got its App ID. I did role assignment for this app as "owner of a particular subscription"

Now from Azure Runbook I want to run my python script and use this App credentials to login. My python script has az cli commands. So I don't want to use powershell command for login. I'm trying to use below command:

Az login --service-principal -u $azureapplucationId -p $azurePassword --tenant $azuretenantId

But I'm not getting any method to convert the azurepassword to secureString.

Is there any way to login using service Principal ID in python script?

Upvotes: 0

Views: 1655

Answers (1)

Jahnavi
Jahnavi

Reputation: 7818

After deploying in my environment, if you want to login using service principal ID in python script, you need to install azure.cli.core module packages in your python script terminal by using

pip install azure.cli.core

Below is my python script taken reference from @Jim Xu, to login into azure cloud using service principal:

   from azure.cli.core import get_default_cli
    az_cli = get_default_cli()
    az_cli.invoke(['login', '--service-principal', '-u', '<app ID/clientID>', '-p', '<clientsecretvalue>','--tenant','<tenantID>'])
  • Create an app->get an app ID

  • Click on app and create a new client secret

    Certifications & secrets->new client secret->copy the obtained client secret value.

  • Under App Registrations > Your app > App Role > Provide the below details:

enter image description here

Output:

You need to run visual studio code as "run as administrator"

enter image description here

Upvotes: 2

Related Questions