Im trying the Patch function on nextjs + googleapis, so far ive tried check my permissions, scopes and credentials, all seems to be in order cause all of the other functions that i have been playin with such as uploading,creating folders, getting raw data are working properly, however when i got to the patching(modifying) something seems off or not working
this is the code im playing with (perflexity assisted me with)
const moveFileOrFolder = async () => {
if (!session || !selectedItemId || !destinationFolderId) return;
try {
const auth = `Bearer ${session.accessToken}`;
// Fetch the current parents of the selected item
const response = await fetch(
`https://www.googleapis.com/drive/v3/files/${selectedItemId}?fields=id,name,parents`,
{
method: "GET",
headers: {
Authorization: auth,
},
}
);
console.log("Response" , selectedItemId)
if (response.ok) {
const data = await response.json();
// Prepare the current parents
const currentParents = data.parents || [];
// Check if the destination folder is already a parent
if (currentParents.includes(destinationFolderId)) {
console.error("The destination folder is already a parent of the selected item.");
return;
}
// Prepare the request to move the file/folder
const updateResponse = await fetch(
`https://www.googleapis.com/drive/v3/files/${selectedItemId }?fields=id,name`,
{
method: "PATCH",
headers: {
Authorization: auth,
"Content-Type": "application/json",
},
body: JSON.stringify({
addParents: destinationFolderId, // Specify the new parent
removeParents: currentParents.join(','), // Specify the current parents to remove
}),
}
);
console.log("Update Response" , destinationFolderId)
if (updateResponse.ok) {
console.log("File/Folder moved successfully!" , updateResponse);
} else {
const errorData = await updateResponse.json();
console.error("Error moving file/folder:", updateResponse.status, errorData);
}
} else {
const errorData = await response.json();
console.error("Error fetching file/folder details:", response.status, errorData);
}
} catch (error) {
console.error("Error moving file/folder:", error);
}
};
return (
<div>
<h2>Move File or Folder</h2>
<br />
<button onClick={fetchFilesAndFolders}>Fetch Files and Folders</button>
{/* Replace the following with actual UI to select a file/folder */}
<br />
<input
type="text"
placeholder="Enter selected file/folder ID"
value={selectedItemId || ""}
onChange={(e) => setSelectedItemId(e.target.value)}
/>
<br />
<input
type="text"
placeholder="Enter destination folder ID"
value={destinationFolderId || ""}
onChange={(e) => setDestinationFolderId(e.target.value)}
/>
<br />
<button onClick={moveFileOrFolder}>Move</button>
</div>
);
};
on my understanding that it should push through, since all the values are being retrieved and sent, I’ve also included add&removeParent parameters, but upon troubleshooting it. It gives out a successful response, even though there are no changes on my google drive folders, even after checking if I have changed the access of the folders from private to public and vice versa to be sure, still the same console log, If anyone can explain and help me out that would be much appreciated