此页面的安全验证无效,可能已损坏。请使用网络浏览器的后退按钮再次尝试您的操作

The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again

我正在尝试为共享点列表创建网络挂钩。不幸的是,这样做的唯一方法是调用 API。所以我调用 API 创建我自己的网络挂钩到我的共享点网站中的自定义列表。

我在 Chrome 中打开了我的 Sharepoint 站点中的开发工具并使用了以下代码:

fetch("https://my-org.sharepoint.com/sites/my-site/_api/web/lists('list-id')/subscriptions", {
  "headers": {
    "accept": "application/json",
    "content-type": "application/json",
  },
  "body": "{\"resource\":\"https://my-org.sharepoint.com/sites/my-site/_api/web/lists('list-id')\",\"notificationUrl\":\"http://my-ngrok-id.ngrok.io\",\"expirationDateTime\":\"2021-11-03T21:54:41.000\"}",
  "method": "POST",
});

但是它给出了一个403错误:

{
    "odata.error": {
        "code": "-2130575251, Microsoft.SharePoint.SPException",
        "message": {
            "lang": "en-US",
            "value": "The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
        }
    }
}

通常,此错误是由于缺少或过期的表单摘要引起的。如果您不使用 OAuth 来授权您的请求,这些操作需要服务器的请求表单摘要值作为 X-RequestDigest header

的值

您可以通过向 http://<site url>/_api/contextinfo 发出空 body 的 POST 请求来检索此值。或者,如果您的代码在 SharePoint 页面中为 运行,则可以直接通过 SharePoint 控件 '#__REQUESTDIGEST' 获取它。

BR

如果在 SharePoint 页面上,请修改 header 以包含 X-RequestDigest。

"headers": {
    "X-RequestDigest": $('#__REQUESTDIGEST').val(),
    "accept": "application/json",
    "content-type": "application/json",
},