使用Google Drive API复制文件时目标文件夹失效问题求助
Google Drive API 复制文件到指定文件夹失败问题解决
核心问题是你把parents参数错误地放到了URL查询字符串中,而Google Drive API的files/copy接口要求parents作为请求体参数传递,而非URL参数。另外,你的代码没有正确传递请求体内容,导致API没有接收到目标文件夹的配置。
修正后的代码
// 要复制的文件ID const fileId = '1g9rSD-file_copied'; // 目标文件夹ID const parentFolderId = '1xu6TIz_my_folder_destination'; const accessToken = "my_access_token_here"; // 拆分参数:supportsAllDrives作为URL查询参数,parents作为请求体参数 const queryParams = new URLSearchParams({ supportsAllDrives: true }).toString(); const copyUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/copy?${queryParams}`; fetch(copyUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${accessToken}`, }, // 正确传递请求体,将目标文件夹ID配置在这里 body: JSON.stringify({ parents: [parentFolderId] }) }) .then(response => response.json()) .then(data => { console.log('复制完成:', data); }) .catch(error => { console.error('请求错误:', error); });
额外排查点
- 确认Access Token权限:至少需要
drive.file权限(推荐),如果涉及共享驱动器,需确保权限覆盖对应资源 - 核对目标文件夹ID:确认文件夹存在,且授权账号拥有该文件夹的写入权限
- 若操作共享驱动器,除了
supportsAllDrives: true,还要确保账号对该共享驱动器有操作权限
内容的提问来源于stack exchange,提问作者Gilvanei Bispo
相关产品推荐
相关产品推荐

