【发布时间】:2022-01-18 19:32:23
【问题描述】:
当我尝试通过 python 列出我在 Azure 上的所有虚拟机时遇到此错误
Code: AuthorizationFailed
Message: The client "XXXX" with object id "XXXX" does not have authorization to perform action 'Microsoft.Compute/virtualMachines/read' over scope '/subscriptions/XXXXX or the scope is invalid. If access was recently granted, please refresh your credentials.
我的代码如下:
from azure.mgmt.compute import ComputeManagementClient
from azure.identity import ClientSecretCredential
Subscription_Id = "XXXX"
Tenant_Id = "XXXXX"
Client_Id = "XXXXX"
Secret = "XXXXX"
credential = ClientSecretCredential(
client_id=Client_Id,
client_secret=Secret,
tenant_id=Tenant_Id
)
compute_client = ComputeManagementClient(credential, Subscription_Id)
vm_list = compute_client.virtual_machines.list_all()
pageobject1 = vm_list.by_page(continuation_token=None)
for page in pageobject1:
for j in page:
print(j)
【问题讨论】:
-
根据错误消息,您的服务主体似乎没有所需的权限。尝试将
Virtual Machine Contributor权限分配给您的 SP。运行 python 脚本并检查您是否能够列出虚拟机 -
@VenkateshDodda-MT 此脚本列出了虚拟机,但出现此错误。同样,当我验证服务主体并将角色分配给我的客户端 ID 时,搜索中也不会显示
标签: python azure azure-active-directory