从SharePoint online删除文件,为什么总是报错403?

Deleting files from SharePoint online, why does this always produce Error 403?

我正在尝试通过 REST 服务从 SharePoint Online 文档库的文件夹中删除文件。我已经在为此解决方案的文件和列表上执行其他几个 CRU(D)-Operations,除了 (D)elete 之外,一切正常。

我尝试了几种访问文件的方法,例如GetFileByServerRelativeUrlGetFileById (=UniqueID)GetFolderByServerRelativeUrl 然后使用 /Files。我也尝试通过 ListItems 来解决它,即使用 /lists/getByTitle('MyTitle')/items(ID)。我是该网站的网站集管理员。我得到了一个 X-RequestDigest,它适用于所有其他操作。 该解决方案是用 TypeScript 和 React 编写的,并使用 axios 进行 http-Requests。

var deleteConfig = {
        headers: {
          "accept": "application/json;odata=verbose",
          "IF-MATCH": "*",
          "X-HTTP-Method": "DELETE",
          "X-RequestDigest": (document.querySelector("#__REQUESTDIGEST") as HTMLInputElement).value
        }
      }

axios.post(this.props.baseUrl + "/_api/web/getfilebyserverrelativeurl('/sites/MySite/DocLib/Test5.txt')/recycle()", deleteConfig)
        .then(response => {
          resolve();
        })
        .catch(ex => {
          if (ex.response.status === 403) {
            alert("Access Denied!");
            console.log(ex);
          } else {
            alert("Error deleting file (" + ex.response.status + "): " + ex);
            console.log(ex);
          }
          reject();
        });

任何删除(有或没有“/recycle()”)都会导致错误 403 和消息 "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."。当所有其他帖子、合并和放置工作正常时,我不明白为什么这个调用会失败。

试试这个代码,使用 JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
    $(function () {
    bindButtonClick();
    });

    function bindButtonClick() {
    $("#btnSubmit").on("click", function () {
    deleteDocument();
    });
    }

    function deleteDocument() {
    var siteUrl = _spPageContextInfo.webAbsoluteUrl;
    var webRelUrl = _spPageContextInfo.webServerRelativeUrl;
    var fullUrl = siteUrl + "/_api/web/GetFileByServerRelativeUrl('" + webRelUrl + "/DocLib/" + "Test5.txt" + "')";

    $.ajax({
    url: fullUrl,
    type: "POST",
    headers: {
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "X-HTTP-Method": "DELETE",
    "IF-MATCH": "*"
    },
    success: onQuerySucceeded,
    error: onQueryFailed
    });
    }

    function onQuerySucceeded() {
    $("#divResults").html("Document successfully deleted!");
    }

    function onQueryFailed(sender, args) {
    alert("Error!");
    }
    </script>