使用python脚本登录Azure CLI的问题

我尝试在azure cli上使用命令python登录,但是我遇到了问题。

脚本代码为:

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.

我使用Visual Studio代码工作

q178455231 回答:使用python脚本登录Azure CLI的问题

我可以使用错误的tenant id重现您的问题,如果我使用正确的问题,则可以解决此问题。另外,我的租户中没有活动订阅。

enter image description here

在门户中导航至Azure Active Directory-> App registrations->找到您的应用并按如下所示复制tenant id,然后使用它再次尝试代码。

enter image description here

,

我使用子进程解决了问题,该子进程调用了外壳程序,然后发送命令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)

此代码对我有用,希望对您有帮助!

本文链接:https://www.f2er.com/3163320.html

大家都在问