Microsoft 身份验证库 (MSAL) 不与 PNPJS 一起使用来创建 SharePoint 列表项

时间:2021-06-24 15:02:59

标签: sharepoint msal spfx

使用下面的文章,我们应该能够访问和更新 SharePoint 数据。 Calling SharePoint via MSAL

但目前我们只能访问数据而不能更新/写入数据到 SharePoint 列表。 这是我们在解决方案中使用的代码片段。

import { MsalClientSetup  } from "@pnp/msaljsclient";
import { sp } from "@pnp/sp/presets/all";

sp.setup({
sp: {
    baseUrl: "https://{my tenant}.sharepoint.com/sites/dev/",
    fetchClientFactory: MsalClientSetup({
        auth: {
            authority: "https://login.microsoftonline.com/mytentant.onmicrosoft.com/",
        clientId: "00000000-0000-0000-0000-000000000000",
        redirectUri: "https://mytentant.sharepoint.com/sites/dev/SitePages/test.aspx",
         },
     }, ["https://mytentant.sharepoint.com/.default"]),
 },
});
const r = await sp.web();

//create list item
await sp.web.lists.getByTitle('FoodOrder')
  .items
  .add({
    Title: 'Chicken Fry',
    Quantity: 5,
    Remarks: 'Extra Spicy'
  })
 .then(async results => {
   console.dir(results);
 })
 .catch(err => {
   console.error(err.message);
 });

以下是通过 Azure APP 授予的权限。 enter image description here

当我们尝试在 SharePoint 列表中创建/更新项目时,它会引发以下错误。

<块引用>

访问被拒绝。 (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))

如果有人对此有解决方案,那将非常有帮助。任何想法将不胜感激,非常感谢。

1 个答案:

答案 0 :(得分:0)

看起来您需要允许您的应用程序写入列表。只需授予它这样做的权限(您需要 AllSites.Write)。 ClientId 不应是“0000000000-00....”,它应该是您的应用程序 ID,即您授予权限的应用程序的 ID。 “0000-...”只是一个占位符。

enter image description here

就像文档说的:

<块引用>

在调用图形 API 时,您必须指定您需要的范围和 确保它们在 AAD 中配置

所以基本上你应该这样做: https://docs.microsoft.com/en-us/graph/auth-register-app-v2

相关问题