我有一个 AAD 应用程序 - ABC,需要通过公开的 api(“api://XYZ/general”)访问其他应用程序 XYZ。我正在尝试使用 MSAL 库并使用 ConfidentialClientApplication 机制,但它不断给我一个错误提示 -
AADSTS500011:在名为 *** 的租户中找不到名为 api://XYZ/general 的资源主体。如果应用程序尚未由租户管理员安装或未得到租户中任何用户的同意,则可能会发生这种情况。您可能已将身份验证请求发送给了错误的租户。
有人可以帮助我解决此错误吗?我已经被阻止了很长一段时间了。
尝试获取访问令牌以使用 Easy Start API。 代码-
authority = app.config["AUTHORITY"] + '/' + app.config["TENANT"]
aadApp = msal.PublicClientApplication(app.config["CLIENT_ID"], authority=authority)
result = None
accounts = aadApp.get_accounts()
if accounts:
# If a user account exists, use it to acquire a token silently
result = aadApp.acquire_token_silent(scopes=app.config["OB_SCOPE"], account=accounts[0])
if not result:
# No user account or token acquisition failed, perform interactive authentication
result = aadApp.acquire_token_interactive(scopes=app.config["OB_SCOPE"])
access_token = result['access_token']
我有一个名为
WebAPI
的应用程序,其中我公开了一个与您具有 相同范围 的 API:
现在,我注册了一个名为
ClientApp18
的 Azure AD 应用程序,并在 API 权限 选项卡中添加了上述权限:
为了生成访问令牌,我通过修改Python代码直接使用了交互流程:
import msal
tenant_id = "fb134080-e4d2-45f4-9562-fxxxxxxxxx0"
client_id = "3b48a780-de28-4576-b1c5-exxxxxxxx2"
scopes = ["api://5bc992e5-b3f8-4cfc-8197-aexxxxxxa/general"]
authority = f"https://login.microsoftonline.com/{tenant_id}/"
aadApp = msal.PublicClientApplication(client_id, authority=authority)
result = aadApp.acquire_token_interactive(scopes=scopes)
if "access_token" in result:
access_token = result['access_token']
print("Access token:", access_token)
else:
print("Interactive authentication failed. Please check your Azure AD configuration.")
当我运行上面的代码时,会打开一个新窗口,选择一个帐户进行登录,如下所示:
确保从应用程序所在的同一租户中选择正确的用户帐户,一旦身份验证成功,您将看到以下屏幕:
身份验证后,我在输出控制台中成功获取了访问令牌:
为了确认这一点,我在 jwt.ms 中解码了上述令牌,并得到了
aud
和 scp
具有正确值的声明:
在您的情况下,请检查您是否已经使用来自不同租户的用户登录,而不是存在应用程序的租户或在代码中传递了错误的
。tenantID
当我通过从不同租户中选择用户或包含错误的
tenantID
来运行代码时,我得到了与您类似的错误:
要解决该错误,请尝试直接使用交互式流程,正如我提到的,通过使用应用程序所在的同一租户的正确的用户帐户登录,并检查您是否使用正确的
tenantID
。
我相信您需要在 Azure AD 应用程序注册中添加 api://XYZ/general 资源的范围。我希望这有帮助。