首页 > 解决方案 > 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 应用程序。

标签: pythonazure

解决方案


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')

推荐阅读