无法授予对具有NodeJS的公共数据服务的访问权限
我正在实现一个简单的节点函数,它将从公共数据服务中获取一些数据。我已经可以获取accessToken了,但是当我使用这个accessToken访问公共数据服务时,响应是“未授权的”。
我遵循了这里的说明( https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/walkthrough-registering-configuring-simplespa-application-adal-js ),并能够让它与简单的页面应用程序一起工作。
我只想将它移植到Node,并让应用程序在不需要用户登录的情况下授予对通用数据服务的访问权限。
const fetch = require('node-fetch');
const AuthenticationContext = require('adal-node').AuthenticationContext;
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const resource = "https://my-org.crm5.dynamics.com";
const clientId = 'my client id';
const clientSecret = 'my client secret';
const authorityHostUrl = 'https://login.microsoftonline.com';
const tenant = 'my-tenant-name.onmicrosoft.com'; // AAD Tenant name.
const authorityUrl = authorityHostUrl + '/' + tenant;
const authContext = new AuthenticationContext(authorityUrl);
const tokenResp = await new Promise((resolve, reject) => {
authContext.acquireTokenWithClientCredentials(resource, clientId, clientSecret, function (err, tokenResponse) {
if (err) {
context.error("cannot get token: " + err.stack);
return reject(err.stack);
} else {
return resolve(tokenResponse);
}
});
});
context.log("tokenResp: ", tokenResp); // The tokenResp contains accessToken
const cdsHeaders = {};
cdsHeaders["Authorization"] = "Bearer " + tokenResp.accessToken;
cdsHeaders["Accept"] = "application/json";
cdsHeaders["Content-Type"] = "application/json; charset=utf-8";
cdsHeaders["OData-MaxVersion"] = "4.0";
cdsHeaders["OData-Version"] = "4.0";
const endpointUrl = encodeURI(resource + "/api/data/v9.0/accounts?$select=name,address1_city&$top=10");
const dataResponse = await fetch(endpointUrl, { method: 'GET', headers: cdsHeaders });
console.log("response: ", dataResponse); // The dataResponse is 401 Unauthorized
context.res = { body: "Done" };
};发布于 2019-01-29 17:38:14
我得到了解决方案:我必须“手动为应用程序应用程序用户创建一个CDS”,才能让它工作,关于这个文档:https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/authenticate-oauth#connect-as-an-app
尽管示例代码是用C#编写的,但C#和Node.js客户端之间没有太多区别。
https://stackoverflow.com/questions/54414551
复制相似问题