![](/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.