gpt4 book ai didi

azure - 尽管 token 正确,但用户未经授权

转载 作者:行者123 更新时间:2023-12-02 08:12:57 24 4
gpt4 key购买 nike

我正在尝试将 D365FO 与第三方应用程序集成,我能够进行正确的设置并注册我的应用程序,获取 token ,如下所示:

enter image description here

我在开发机器上使用资源作为D365FO的链接,即https://usnconeboxax1aos.cloud.onebox.dynamics.com/并阅读D365FO数据如下图:

enter image description here

我想更改登录方法,因此我使用密码代表用户登录,因此我禁用了 Azure 上的安全详细信息(基于 this ),如下所示:

enter image description here

并成功获取登录用户的 token ,如下所示:

enter image description here

enter image description here

但是,当我尝试获取之前能够获取的相同数据时,尽管我使用 Admin 帐户登录,但还是收到了 401 未经授权的错误:

enter image description here

最佳答案

如果您调用具有无效受众的资源,通常会出现401 Unauthorized错误。

当您生成 scopeuser.read openid profileoffline_access 的访问 token 时,受众将为 Microsoft Graph,这将不起作用与 D365FO。

我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:

我注册了一个 Azure AD 应用程序并添加了相同的 API 权限,如下所示:

enter image description here

现在,我通过包含相同参数,生成了授予类型为password的 token ,如下所示:

POST https://login.microsoftonline.com/organizations/oauth2/v2.0/token

client_id: <appID>
client_secret: <secret>
scope: user.read openid profile offline_access
grant_type: password
username: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbdb8b1b5b29ca4a4a4a4a4a4a4a4a4f2b3b2b1b5bfaeb3afb3baa8f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>
password: xxxxxxxxxxx

回应:

enter image description here

您可以通过将上述访问 token 粘贴到 jwt.ms 来解码以检查受众。

当我解码访问 token 时,我得到 aud 声明为 00000003-0000-0000-c000-000000000000(即 Microsoft Graph),如下所示:

enter image description here

如果您使用此 token 读取 D365FO 数据,您将收到 401 Unauthorized 错误,因为受众无效。

解决该错误,您需要通过进行以下更改来生成访问 token ,其中 resource 值作为 D365FO 实例的基本 URL:

POST https://login.microsoftonline.com/organizations/oauth2/token

client_id: <appID>
client_secret: <secret>
resource: <base URL of your D365FO instance without the trailing '/'>
grant_type: password
username: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0160656c686f417979797979797979792f6e6f6c6862736e726e67752f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>
password: xxxxxxxxxxx

在您的情况下,资源参数的值应为https://usnconeboxax1aos.cloud.onebox.dynamics.com

此 token 的受众与您的 D365FO 根 URL 相同。为了确认这一点,您可以在 jwt.ms 中对其进行解码。如果你用这个token读取D365FO数据,就可以了!

引用:

Test services by using third-party utilities - Finance & Operations | Dynamics 365 | Microsoft

关于azure - 尽管 token 正确,但用户未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75274922/

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