简体   繁体   English

使用 python 脚本登录 azure cli 的问题

[英]issue to login on azure cli with python script

I tried to login with commands azure cli on python , but i have a issue.我尝试在python上使用命令azure cli登录,但我遇到了问题。

the script code is:脚本代码是:

from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
    client_id = 'a1bc23d4-e5fg-6hi7-8901-23456j7kl8mn',
    secret = '112233445566',
    tenant = 'z0y987x-6543-2w1v-0987-6u5t4s32109r'
   )

msrest.except ions.AuthenticationError: , AdalError: Get Token request returned http error: 400 and server response: {"error":"invalid_request","error_description":"ZZBXQH1775: Tenant 'z0y987x-6543-2w1v-0987-6u5t4s32109r' not found. This may happen if there are no active subscriptions for the tenant. Check with your subscription administrator.

i use visual studio code to work我使用 Visual Studio 代码工作

I can reproduce your issue with a wrong tenant id , if I use the correct one, it works.我可以用错误的tenant id重现您的问题,如果我使用正确的,它可以工作。 Also, there is no active subscription in my tenant.此外,我的租户中没有有效订阅。

在此处输入图像描述

Navigate to the Azure Active Directory in the portal -> App registrations -> find your app and copy the tenant id as below, then use it to try the code again.在门户中导航到Azure Active Directory -> App registrations -> 找到您的应用并复制以下tenant id ,然后使用它再次尝试代码。

在此处输入图像描述

I solved my issue using a subprocess,this subprocess call the shell and then send the command az cli我使用子进程解决了我的问题,这个子进程调用 shell 然后发送命令 az cli

For example:例如:

import subprocess
from subprocess import Popen
from subprocess import PIPE
bckp = Popen('az sql db list -s sqlserver --resource-group MYResourceGroup -o table',  shell=True ,stdout=subprocess.PIPE)
text = bckp.stdout.read().decode("ascii") 
print(text)

with open("file.txt","w") as f:
    f.write(text)

This code works for me, I hope helpfull for you!!这段代码对我有用,希望对你有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM