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
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:
Response:
To resolve the error, you need to pass Site ID of your SharePoint site that can be found with below API call:
Response:
When I ran the API again by passing SharePoint site ID, I got the response successfully with files and folders details like this:
Response:
In your case, replace SharePoint site name with
site ID
in the URL to resolve the error.You can access site either by a relative path or by a site id.
The URL template to access site by relative path is
The response will contain the id of the site.
It’s more common to use site id in the Graph API queries