Azure Python SDK:'ServicePrincipalCredentials' 对象没有属性 'get_token'
Azure Python SDK: 'ServicePrincipalCredentials' object has no attribute '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 库目前正在更新以共享常见的云模式,例如身份验证协议、日志记录、跟踪、传输协议、缓冲响应和重试。
这也会稍微改变身份验证机制。在旧版本中,azure.common
中的 ServicePrincipalCredentials
用于向 Azure 进行身份验证并创建服务客户端。
在较新的版本中,身份验证机制已被 re-designed 替换为 azure-identity
库,以便为所有 Azure SDK 提供基于 Azure Identity 的统一身份验证。 运行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
方法和 compute_client
照常列出所有虚拟机:
# List all Virtual Machines in the specified subscription
def list_virtual_machines():
for vm in compute_client.virtual_machines.list_all():
print(vm.name)
list_virtual_machines()
参考文献:
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_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"]
)
所以我有以下 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 库目前正在更新以共享常见的云模式,例如身份验证协议、日志记录、跟踪、传输协议、缓冲响应和重试。
这也会稍微改变身份验证机制。在旧版本中,azure.common
中的 ServicePrincipalCredentials
用于向 Azure 进行身份验证并创建服务客户端。
在较新的版本中,身份验证机制已被 re-designed 替换为 azure-identity
库,以便为所有 Azure SDK 提供基于 Azure Identity 的统一身份验证。 运行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
方法和 compute_client
照常列出所有虚拟机:
# List all Virtual Machines in the specified subscription
def list_virtual_machines():
for vm in compute_client.virtual_machines.list_all():
print(vm.name)
list_virtual_machines()
参考文献:
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_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"]
)