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.

我们注意到,我们可以通过在构造函数中包含resource='https://graph.windows.net'来避免使用ServicePrincipalCredentials时出现此错误,但是在使用get_azure_cli_credentials()时似乎没有等效的方法可以做到这一点。

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

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

get_azure_cli_credentials

目前确实无法为您提供具有与 ARM 不同的"资源"定义的凭据类(现在是: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')

您可以立即获取凭据和tenant_id,并为此创建客户端行:

from azure.common.credentials import get_azure_cli_credentials
from azure.graphrbac import GraphRbacManagementClient
cred, _, tenant_id = get_azure_cli_credentials(
resource='https://graph.windows.net',
with_tenant=True)
client = GraphRbacManagementClient(cred, tenant_id)

这就像一个魅力。

请注意,此 API 已弃用。优先使用新的Microsoft图(而不是 Azure AD 图(。

最新更新