错误401 Unauthorized
如果您使用以下命令调用资源,通常会发生invalid观众。
当您生成访问令牌时scope
as user.read openid配置文件offline_access,观众将微软图谱这不适用于 D365FO。
我尝试通过 Postman 在我的环境中重现相同的结果,并得到以下结果:
我注册了一个 Azure AD 应用程序并添加了相同的应用程序API权限像下面这样:
现在我生成了授予类型为的令牌password
通过包括same参数如下:
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: [email protected] /cdn-cgi/l/email-protection
password: xxxxxxxxxxx
回复:
您可以通过将上面的访问令牌粘贴到中来对其进行解码jwt.ms https://jwt.ms/来检查观众。
当我解码访问令牌时,我得到了aud声称为00000003-0000-0000-c000-000000000000
(即 Microsoft Graph)如下所示:
如果您使用此令牌读取D365FO数据,您将得到401 未经授权错误,因为观众无效。
To resolve错误,您需要生成访问令牌resource
价值为base URL通过进行以下更改来更改您的 D365FO 实例:
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: [email protected] /cdn-cgi/l/email-protection
password: xxxxxxxxxxx
在你的情况下,价值resource参数应该是https://usnconeboxax1aos.cloud.onebox.dynamics.com
该令牌将具有audience与您的 D365FO 根 URL 相同。为了确认这一点,您可以将其解码jwt.ms https://jwt.ms/。如果你用这个token读取D365FO数据,就可以了!
参考:
使用第三方实用程序测试服务 - 财务和运营 |动态 365 |微软 https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/third-party-service-test#prerequisites