python - GraphRbacManagementClient.applications.create() 返回访问令牌丢失或格式错误
问题描述
我们正在尝试使用 Python SDK (v2.0) 和当前用户的 CLI 凭据创建 Azure 应用程序注册。
from azure.common.credentials import get_azure_cli_credentials
from azure.graphrbac import GraphRbacManagementClient
credentials, subscription_id = get_azure_cli_credentials()
client = GraphRbacManagementClient(credentials, 'my-tenant-id')
app_parameters = {
'available_to_other_tenants': False,
'display_name': 'my-app-name',
'identifier_uris': ['http://my-app-name.com']
}
app = client.applications.create(app_parameters)
但这会返回
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/my-app-code/.venv/lib/python3.6/site-packages/azure/graphrbac/operations/applications_operations.py", line 86, in create
raise models.GraphErrorException(self._deserialize, response)
azure.graphrbac.models.graph_error.GraphErrorException: Access Token missing or malformed.
我们注意到,我们可以ServicePrincipalCredentials
通过在构造函数中包含来避免这个错误resource='https://graph.windows.net'
,但是在使用get_azure_cli_credentials()
.
我们做错了什么,还是应该这样做?
请不要回复我们应该使用ServicePrincipalCredentials
. 我们的用例明确表示交互式用户可以使用 Python SDK 创建/注册 Azure 应用程序。
解决方案
get_azure_cli_credentials
目前确实无法为您提供具有不同于 ARM 的“资源”定义的 Credentials 类(现在是:azure-common 1.1.10 及以下版本)
您可以通过以下方式解决:
from azure.common.credentials import get_cli_profile
profile = get_cli_profile()
cred, subscription_id, _ = profile.get_login_credentials(resource='https://graph.windows.net')
请在https://github.com/Azure/azure-sdk-for-python上创建一个问题,并提供指向此 SO 的链接,我将尝试为下一个版本的 azure-common 执行此操作。
(我在 MS 工作并拥有此代码)
编辑:已发布 1.1.11 的一部分 https://pypi.org/project/azure-common/1.1.11/
from azure.common.credentials import get_azure_cli_credentials
cred, subscription_id = get_azure_cli_credentials(resource='https://graph.windows.net')
推荐阅读
- oracle - 通过在 PL/SQL 中引发异常来插入详细信息的过程
- javascript - 在 Object 中查找最大元素及其键 - React native
- ios - 元素树层次结构中缺少 Xcode 按钮
- python - 如何使用 ExecuteScript 和 python 从 nifi 中的一个传入流文件创建多个流文件
- python - Python subprocess.Popen:使用 shell=True 运行多个命令,但似乎并没有等待所有命令完成
- c++ - opencv + cuda 如何以 64 fps 从屏幕录制视频?
- indexeddb - Rxdb:通过手动复制(复制)rxdb 数据库来清除已删除的项目和旧修订?
- reactjs - 如何在 reactjs 中使用 vanilla js 创建图表?
- java - 使用 GTK 在 linux 上使用触摸屏和 openjfx 滚动问题
- java - 如何检查文件是否在 AWS S3 Bucket 上被删除