Rest 403 禁止 SharePoint 框架应用程序

Rest 403 Forbidden SharePoint Framework Application

我正在尝试学习 React 并正在创建 SharePoint 框架 Web 部件应用程序。我正在尝试使用 fetch from react 从网站上的列表中检索数据。我在托管 SharePoint workbench 中工作。

当我 运行 通过单击按钮执行此操作时,我收到 403 禁止。当我在邮递员中设置相同的休息电话时,我得到了预期的返回数据。我相信我需要将 X-RequestDigest 添加到 header,但是 document.getElementById("__REQUESTDIGEST") returns 为空。我在 SharePoint 中做错了什么 workbench?

submit = e => {
 fetch(
  "https://MY_URL_HERE.sharepoint.com/_api/lists/getbytitle('MY_LIST_NAME_HERE')/items",
  {
    method: "GET",
    headers: {
      "Accept": "application/json;odata=verbose",
      "Content-Type": "application/json;odata=verbose",
      "odata-version": "",
      "IF-MATCH": "*",
      "X-HTTP-Method": "MERGE",
    }
  }
 ).then(response => console.log(response));
};

SPFx 提供内置 httpclient 来进行 REST API 调用。

不,如果您在 SharePoint 中托管 workbench,那么,要获取列表项,您不需要请求摘要。

按如下所述修改您的代码:

添加以下导入语句:

import { SPHttpClient, SPHttpClientResponse, SPHttpClientConfiguration } 
from '@microsoft/sp-http';

现在,您可以使用以下代码代替获取:

this.context.spHttpClient.get("https://MY_URL_HERE.sharepoint.com/_api/lists/getbytitle('MY_LIST_NAME_HERE')/items",  
      SPHttpClient.configurations.v1)  
      .then((response: SPHttpClientResponse) => {  
        response.json().then((responseJSON: any) => {  
          console.log(responseJSON);  
        });  
      });