在运行 Chaos Toolkit Azure 相关的 Playbooks 时,我收到错误消息“失败:AttributeError: 'ServicePrincipalCredentials' object has no attribute 'get_token” 尽管我已经传入了正确的 Secret ID 、 Tenant ID 、 Client ID 、 SUBscription ID 和 Client Secret ID .. 甚至订阅也拥有对服务主体的完全权限。
问问题
304 次
1 回答
0
简短回答:这将有助于检查您尝试运行的确切代码,但从提到的错误来看,您似乎正在尝试以旧方式使用较新的库。使用基于 的较新 SDK 库时azure.core
,请使用库中的ClientSecretCredential
对象azure.identity
。使用较旧的 SDK 库时,ServicePrincipalCredentials
请从azure.common
库中使用。
长答案: 用于 Python 的 Azure 库目前正在更新以共享常见的云模式,例如身份验证协议、日志记录、跟踪、传输协议、缓冲响应和重试。
在较新的版本中,身份验证机制已被重新设计并被azure-identity
库替换,以便为所有 Azure SDK 提供基于 Azure Identity 的统一身份验证。跑去pip install azure-identity
拿包。
因此,不要使用库中的ServicePrincipalCredentials
类,而是使用如下方式:azure.common
ClientSecretCredential
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient
credential = ClientSecretCredential(
tenant_id='xxxxx',
client_id='xxxxx',
client_secret='xxxxx'
)
这是另一个具有详细解决方案的相关线程:Azure Python SDK:'ServicePrincipalCredentials' object has no attribute 'get_token'
附加参考:
- GitHub 上适用于 Python 的 Azure SDK
- 使用令牌凭据进行身份验证
于 2021-03-17T14:57:25.117 回答