gpt4 book ai didi

python - 如何使用服务主体和 Python SDK 向 Azure 进行身份验证?

转载 作者:行者123 更新时间:2023-12-02 07:58:04 26 4
gpt4 key购买 nike

我当前正在尝试使用 azure-mgmt-support 向 Azure 进行身份验证MicrosoftSupport 客户端并收到以下错误:

AdalError: Get Token request returned http error: 400 and server response: {"error":"unauthorized_client","error_description":"AADSTS700016: Application with identifier 'xxx' was not found in the directory 'management.core.windows.net'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.

我已经仔细检查过,并且确实使用了正确的 client_idtenant_id。我在这里缺少什么?我的代码如下:

from azure.mgmt.support import MicrosoftSupport
from msrestazure.azure_active_directory import ServicePrincipalCredentials

sub_id = 'xxx'
sp_creds = ServicePrincipalCredentials(client_id='xxx', secret='xxx')

SupportClient = MicrosoftSupport(sp_creds, sub_id)

最佳答案

经过一小段步行并再次查看文档后,我发现了错误 - 我缺少 ServicePrincipalCredentials 对象中的 tenant_id。不明显from the SDK specification或错误消息表明这是缺少的,因为唯一需要的变量是 client_idsecret,但是当我查看 this example in the documentation 时我意识到它丢失了(为后代粘贴下面的代码,以防文档页面发生更改)。

import os
from azure.mgmt.resource import SubscriptionClient
from azure.common.credentials import ServicePrincipalCredentials

# Retrieve the IDs and secret to use with ServicePrincipalCredentials
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
tenant_id = os.environ["AZURE_TENANT_ID"]
client_id = os.environ["AZURE_CLIENT_ID"]
client_secret = os.environ["AZURE_CLIENT_SECRET"]

credential = ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=client_secret)

subscription_client = SubscriptionClient(credential)

subscription = next(subscription_client.subscriptions.list())
print(subscription.subscription_id)

关于python - 如何使用服务主体和 Python SDK 向 Azure 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62049965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com