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

2024-09-30 02:16:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python上使用azure cli命令登录,但有一个问题。在

脚本代码是:

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.

我使用visualstudio代码工作


Tags: 代码fromimport命令脚本clientidcli
2条回答

我可以用一个错误的tenant id来重现你的问题,如果我使用正确的一个,它就可以工作了。而且,我的租户中没有活动订阅。在

enter image description here

导航到门户网站中的Azure Active Directory->;App registrations->;找到你的应用程序并复制下面的{},然后使用它重试代码。在

enter image description here

我解决了我的问题使用一个子进程,这个子进程调用shell,然后发送命令az cli

例如:

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)

这个代码对我有用,希望对你有帮助!!在

相关问题 更多 >

    热门问题