[英](msal-node) - Dynamics 365 Business Central returns 401 for any resource with received token
我们目前正在切换到基于@azure/msal-node
package 的基于 OAuth 的授权解决方案,以使用 Dynamics 365 Business Central(19.5,云)提供的 API 授权我们的 API。 不知何故我无法让它工作。 当使用检索到的令牌请求任何资源时,我们的租户实例仅返回 401。
真的在这里走到了死胡同,任何帮助将不胜感激。
在 Azure 的“应用程序注册”部分中设置的权限
是的,我已代表我们的租户同意所有这些权限(管理员同意)
在jwt.ms处解码时,令牌甚至包含以下范围:
{
"scopes": [
"Automation.ReadWrite.All",
"app_access",
"API.ReadWrite.All"
]
}
我们的实施示例
/**
* Uses `msal-node` to authenticate against the microsoft servers
* to gain access to Dynamics 365 Business Central
*/
async getClientCredentialsToken() {
try {
const validity = await this.validateClientCredentialsToken();
if (validity) return;
const authOptions = {
clientId: process.env.AAD_CLIENT_ID,
authority: process.env.AAD_AUTHORITY,
clientSecret: process.env.AAD_CLIENT_SECRET,
};
const cacheOptions = {
Account: {},
IdToken: {},
AccessToken: {},
RefreshToken: {},
AppMetadata: {},
};
const cca = new msal.ConfidentialClientApplication({
cache: cacheOptions,
auth: authOptions,
});
const response = await cca.acquireTokenByClientCredential({
azureRegion: null,
skipCache: true,
scopes: ["https://api.businesscentral.dynamics.com/.default"],
});
return response;
} catch (err) {
console.log(err);
}
},
请求现有端点时的响应示例
无论命中哪个端点。 这是我得到的回应:
{
"error": {
"code": "Authentication_InvalidCredentials",
"message": "The server has rejected the client credentials. CorrelationId: b004d293-f576-40c9-bbc6-3fb32533a65b."
}
}
在文档中找到的解决方案
因此,对于遇到相同情况的任何人,我都找到了最终有效的方法! 您需要按照官方文档中的描述在Dynamics 365 Business Central 客户端中注册应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.