skip to Main Content

I’m using graph API in my react project to access SharePoint files in a site called test-site-001. The main functionality is to get all the files and folder details. Below is my HTTP request and the code.

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);
  }
};

Also, I’m getting the access token from Azure AD. Below are the scopes.

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

And I’m getting the below error.

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

I’m new to the Graph API, so appreciate the help.

2

Answers


  1. The error occurred as you are passing SharePoint site name instead of site ID in the API call to retrieve files.

    I have one SharePoint site named sridemosite with below files and folders in it:

    enter image description here

    When I ran below API call by passing SharePoint site name to retrieve files and folders via Graph Explorer, I too got same error:

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

    Response:

    enter image description here

    To resolve the error, you need to pass Site ID of your SharePoint site that can be found with below API call:

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

    Response:

    enter image description here

    When I ran the API again by passing SharePoint site ID, I got the response successfully with files and folders details like this:

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

    Response:

    enter image description here

    In your case, replace SharePoint site name with site ID in the URL to resolve the error.

    Login or Signup to reply.
  2. You can access site either by a relative path or by a site id.

    The URL template to access site by relative path is

    GET https://graph.microsoft.com/v1.0/sites/{your_tenant_name}.sharepoint.com:{your_site_name}
    

    The response will contain the id of the site.

    const fetchOneDriveFiles = async () => {
      try {
        const response = await fetch(
          "https://graph.microsoft.com/v1.0/sites/{your_tenant_name}.sharepoint.com: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);
      }
    };
    

    It’s more common to use site id in the Graph API queries

    const fetchOneDriveFiles = async () => {
      try {
        const response = await fetch(
          "https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root/children",
          {
            headers: {
              Authorization: `Bearer ${accessToken}`,
            },
          }
        );
        const data = await response.json();
        setFiles(data.value);
      } catch (error) {
        console.error("Error fetching files:", error);
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search