我正在尝试使用 nextjs + googleapis 创建补丁功能以将特定文件/文件夹移动到另一个文件夹
这是我正在尝试的代码:
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/${destinationFolderId}?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);
}
};
在检查或错误处理它时,它给了我一条消息,要求添加Parent和removeParent,我已经添加了它,并且我能够收到一条消息,表明文件?文件夹已被移动,但是在检查我的谷歌驱动器时没有移动的文件夹。我还检查了我的 Api 路由(添加了 Patch 的范围)。
如果有人可以帮助我了解如何使其工作以及为什么它显示消息但没有任何更改,我将不胜感激
我把它修好了,我只是不理解/不知道关于路径参数和查询参数的谷歌文档,所以我一直在尝试在正文请求中使用 addParent 和 removeParent ,但它应该在 Path 参数中.
所以现在一切正常