使用 Microsoft Graph API 访问 SharePoint 中的文件时如何修复此租赁错误的主机名无效

问题描述 投票:0回答:1

我在 React 项目中使用图形 API 来访问名为

test-site-001
的网站中的 SharePoint 文件。主要功能是获取所有文件和文件夹的详细信息。下面是我的 HTTP 请求和代码。

const fetchOneDriveFiles = async () => {
  try {
    const response = await fetch(
      "https://graph.microsoft.com/v1.0/sites/test-site-001/drive/root/children",
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      }
    );
    const data = await response.json();
    setFiles(data.value);
  } catch (error) {
    console.error("Error fetching files:", error);
  }
};

此外,我还从 Azure AD 获取访问令牌。以下是范围。

scopes: ["User.Read", "Files.Read", "Sites.Read.All", "Sites.ReadWrite.All"]

我收到以下错误。

error: 
{
   code: "invalidRequest", 
   message: "Invalid hostname for this tenancy"
}

我是 Graph API 的新手,非常感谢您的帮助。

azure azure-active-directory active-directory microsoft-graph-api microsoft-entra-id
1个回答
0
投票

当您在 API 调用中传递 SharePoint 站点名称而不是站点 ID 来检索文件时,发生了错误。

我有一个名为

sridemosite
的 SharePoint 网站,其中包含以下文件和文件夹:

enter image description here

当我通过传递 SharePoint 站点名称来运行 API 调用以通过 Graph Explorer检索文件和文件夹时,我也遇到了同样的错误

GET https://graph.microsoft.com/v1.0/sites/site_name/drive/root/children

回复:

enter image description here

要解决该错误,您需要传递 SharePoint 网站的网站 ID,可以通过以下 API 调用找到该网站:

GET https://graph.microsoft.com/v1.0/sites/root:/sites/site_name

回复:

enter image description here

当我通过传递 SharePoint 站点 ID 再次运行 API 时,我成功获得了 响应,其中包含如下文件和文件夹详细信息:

GET https://graph.microsoft.com/v1.0/sites/site_ID/drive/root/children

回复:

enter image description here

在您的情况下,请将 URL 中的 SharePoint 站点名称 替换为

site ID
以解决错误。

© www.soinside.com 2019 - 2024. All rights reserved.