英文:
Getting error - AADSTS500011: The resource principal named ** was not found in the tenant named **
问题
我有一个AAD应用程序 - ABC,需要通过一个暴露的API访问其他应用程序XYZ,该API是“api://XYZ/general”。我正在尝试使用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:
# 如果存在用户帐户,则使用它来静默获取令牌
result = aadApp.acquire_token_silent(scopes=app.config["OB_SCOPE"], account=accounts[0])
if not result:
# 没有用户帐户或令牌获取失败,执行交互式身份验证
result = aadApp.acquire_token_interactive(scopes=app.config["OB_SCOPE"])
access_token = result['access_token']
错误-
点击此处查看图片描述
英文:
I have an AAD Application - ABC, that needs to access other application XYZ via an exposed api which is - "api://XYZ/general". I`m trying to use MSAL library and using ConfidentialClientApplication mechanism,but it is constantly giving me an error stating -
AADSTS500011: The resource principal named api://XYZ/general was not found in the tenant named ***. 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 might have sent your authentication request to the wrong tenant.
Can someone please assist me on how to resolve this error? I have been blocked on this for quite some time now.
Trying to get access token to use Easy Start APIs.
Code-
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']
Error-
enter image description here
答案1
得分: 1
我有一个名为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
。
英文:
I have one application named WebAPI
where I exposed an API with same scope as you:
Now, I registered one Azure AD application named ClientApp18
and added above permissions in API permissions tab:
To generate access token, I directly used interactive flow by modifying your python code:
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.")
When I ran the above code, a new window opened to pick an account to sign in like this:
Make sure to select right user account from same tenant where the applications exist and you will get below screen once Authentication is successful:
After authenticating, I got the access token successfully in the output console:
To confirm that, I decoded the above token in jwt.ms and got aud
& scp
claims with correct values:
> In your case, check whether you are already logged in with user from different tenant other than the tenant where applications exist or passed wrong tenantID
in code.
When I ran the code by selecting user from different tenant or including wrong tenantID
, I got similar error as you:
To resolve the error, try to use interactive flow directly as I mentioned by signing in with right user account from same tenant where the applications exist and check whether you are using right tenantID
or not.
答案2
得分: 0
我相信您需要在Azure AD应用程序注册中为您的api://XYZ/general资源添加范围。这是链接:https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-expose-web-apis#add-a-scope。希望这有所帮助。
英文:
I believe you need to add a scope for your api://XYZ/general resource in Azure AD application registration. Here is the link for it: https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-expose-web-apis#add-a-scope. I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论