![](/img/trans.png)
[英]I am getting an internal server error on my local Python server when authenticating to Yelp API
[英]I am authenticating to azure through python to list down all my virtual machines and I am getting this error
当我尝试列出 Azure 到 python 上的所有虚拟机时,出现此错误
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)
当您尝试将virtualmachinecontributor
等特定角色分配给您的服务主体时,您需要传递service principal/appregistration name
,而不是传递您的应用程序注册applicationId/objectId
,如下所示。
service principal/appregistration
所需的访问权限后,您将能够提取订阅中的虚拟机列表。 我们已经在我们的本地环境中检查了上述 python,它也运行良好。这是示例 output 屏幕截图以供参考:
更新了使用资源管理客户端提取 VM 列表的答案:
from azure.mgmt.resource import ResourceManagementClient
from azure.identity import ClientSecretCredential
Subscription_Id = "<subId>"
Tenant_Id = "<tenantid>"
Client_Id = "<appId>"
Secret = "<clientSecret>"
credential = ClientSecretCredential(
client_id=Client_Id,
client_secret=Secret,
tenant_id=Tenant_Id
)
resource_client=ResourceManagementClient(credential=credential,subscription_id=Subscription_Id)
resource_list=resource_client.resources.list()
for item in resource_list:
if(item.type == 'Microsoft.Compute/virtualMachines'):
print(item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.