GraphRbacManagementClient.applications.create() 返回访问令牌丢失或格式错误

Mat*_*one 1 python azure

我们正在尝试使用 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)
Run Code Online (Sandbox Code Playgroud)

但这返回

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.
Run Code Online (Sandbox Code Playgroud)

我们注意到我们可以ServicePrincipalCredentials通过resource='https://graph.windows.net'在构造函数中包含来避免这个错误,但是在使用get_azure_cli_credentials().

我们做错了什么,还是应该这样做?

请不要回复我们应该使用ServicePrincipalCredentials. 我们的用例明确指出交互式用户可以使用 Python SDK 创建/注册 Azure 应用程序。

Lau*_*uel 5

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')
Run Code Online (Sandbox Code Playgroud)

请在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')
Run Code Online (Sandbox Code Playgroud)