尝试在Sharepoint add中通过javascript获取Sharepoint rest api调用的访问令牌,面



我们正在尝试创建一个Sharepoint插件。在那里,我需要生成一些Sharepoint rest api调用访问令牌。我能够从邮差生成,但在Sharepoint Add中从Javascript代码中生成时,它显示了一个CORS策略错误。错误如下:

从原点'https://DomainName-d9cbe540d4fdef.sharepoint.com'获取'https://accounts.accesscontrol.windows.net/{TenantID}/tokens/oAuth/2'的访问已被CORS策略阻止:对预飞行请求的响应未通过访问控制检查:请求的资源上不存在'Access- control - allow - origin '标头。如果一个不透明的响应满足你的需要,设置请求的模式为'no-cors'来获取CORS禁用的资源。

const tokenRequest = {
grant_type: "client_credentials",
client_id: "clientID@TenantID",
client_secret: "Secret Value",
resource: "00000003-0000-0ff1-ce00-000000000000/domain@tenantID",
};
const endpoint = 'https://accounts.accesscontrol.windows.net/{TenantID}/tokens/oAuth/2';
// Make token request and handle response
fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
crossorigin: true,
mode: 'no-cors'
},
body: new URLSearchParams(tokenRequest),
})
.then(response => response.json())
.then(data => console.log(data.access_token))
.catch(error => console.error(error));

在javascript中生成Token用于调用sharepoint rest api

制作" app authentication"(这意味着使用凭据具有自己的权限,而不是使用连接的用户上下文)是一个很大的禁忌,因为它是一个安全打开的大门,因为您必须在前端开发中公开存储凭据。

在这种情况下,你必须有一个后端来发出请求:Azure功能,Web应用程序,树莓派上的服务器,逻辑应用程序,电力自动化,…无论您决定什么,但后端必须制作"应用程序"。调用。

浏览器环境中所期望的实际上是Microsoft宇宙中的三条腿身份验证,这意味着您希望使用已连接的用户以他的名义进行查询。要做到这一点,您必须使用OAuth和AAD应用程序才能被允许使用用户上下文。

最新更新