如何解决Azure Python SDK:“ ServicePrincipalCredentials”对象没有属性“ get_token”
因此,我具有以下Python3脚本来列出所有虚拟机。
import os,json
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.resource import ResourceManagementClient,SubscriptionClient
from azure.common.credentials import ServicePrincipalCredentials
credentials = ServicePrincipalCredentials(
client_id="xxx",secret="xxx",tenant="xxx"
)
resource_client = ResourceManagementClient(credentials,"my-subscription")
compute_client = ComputeManagementClient(credentials,"my-subscription")
network_client = NetworkManagementClient(credentials,"my-subscription")
for vm in compute_client.virtual_machines.list_all():
print("\tVM: {}".format(vm.name))
但是由于某些原因,出现以下错误:
Traceback (most recent call last):
File "/Users/me/a/azure-test.py",line 17,in <module>
for vm in compute_client.virtual_machines.list_all():
...
File "/usr/local/lib/python3.8/site-packages/azure/core/pipeline/policies/_authentication.py",line 93,in on_request
self._token = self._credential.get_token(*self._scopes)
AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token'
我做错什么了吗?
解决方法
Python的Azure库当前正在更新,以共享常见的云模式,例如身份验证协议,日志记录,跟踪,传输协议,缓冲的响应和重试。
这也会稍微改变身份验证机制。在旧版本中,ServicePrincipalCredentials
中的azure.common
用于向Azure进行身份验证并创建服务客户端。
在较新的版本中,身份验证机制已经过重新设计,并被azure-identity
库代替,以便为所有Azure SDK提供基于Azure身份的统一身份验证。运行pip install azure-identity
来获取软件包。
就代码而言,然后是:
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
credentials = ServicePrincipalCredentials(
client_id='xxxxx',secret='xxxxx',tenant='xxxxx'
)
compute_client = ComputeManagementClient(
credentials=credentials,subscription_id=SUBSCRIPTION_ID
)
现在是:
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
credential = ClientSecretCredential(
tenant_id='xxxxx',client_id='xxxxx',client_secret='xxxxx'
)
compute_client = ComputeManagementClient(
credential=credential,subscription_id=SUBSCRIPTION_ID
)
然后您可以将list_all
的{{1}}方法与往常一样列出所有VM:
compute_client
参考:
- Azure SDK for Python在GitHub上
- Migration Guide - Resource Management
- How to authenticate and authorize Python apps on Azure
- Example: Use the Azure libraries to provision a virtual machine
如果是 Azure 主权云(AZURE_PUBLIC_CLOUD、AZURE_CHINA_CLOUD、AZURE_US_GOV_CLOUD、AZURE_GERMAN_CLOUD),接受的答案将扩展到下面的代码片段。
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
from msrestazure.azure_cloud import AZURE_US_GOV_CLOUD as cloud_env
credential = ClientSecretCredential(
tenant_id='xxxxx',client_secret='xxxxx',authority=cloud_env.endpoints.active_directory
)
compute_client = ComputeManagementClient(
credential=credential,subscription_id=SUBSCRIPTION_ID
base_url=cloud_env.endpoints.resource_manager,credential_scopes=[cloud_env.endpoints.resource_manager + ".default"]
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。