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.
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:
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:
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:
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:
In your case, replace SharePoint site name with site ID
in the URL to resolve the error.