首页 > 解决方案 > 使用 Google Drive API 将文件夹移动到团队云端硬盘

问题描述

我正在将我的云端硬盘迁移到团队云端硬盘,并且我有几千个文件夹要移动,每个文件夹都放入自己的云端硬盘。我有一些 php 代码google/apiclient:2.7可以使用 Google Drive API 来创建驱动器。我现在需要移动文件夹并且在这样做时遇到错误。

我的代码正在使用 OAuth 对具有适当权限并能够使用 Google UI 迁移文件夹的用户进行身份验证。当我使用 php 代码将文件移动到团队驱动器时,它移动成功。当我尝试对文件夹执行此操作时,我收到一条错误消息。

代码:

//$files is an array where the values are the google id's
$id = $files['File adI3O'];
$parentId = $files['Drive'];

$google_file = new Google_Service_Drive_DriveFile();
//The parents field is not directly writable in update requests. Use the addParents and removeParents parameters instead.
//$google_file->setParents([$parentId]);

/** @var Google_Service_Drive $service */
$response = $service->files->update($id, $google_file, $service->optParams([
    'supportsAllDrives' => true,
    'addParents' => $parentId,
//'useDomainAdminAccess' => true,//(update) unknown parameter: 'useDomainAdminAccess'
]));

(我的代码中没有 removeParents 参数。我在不使用 removeParents 参数的情况下成功移动了文件,因此一定不需要它。)

将 $id 设置为时的错误消息$files['Folder 69daL']

Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "teamDrivesFolderMoveInNotSupported",
    "message": "Moving folders into shared drives is not supported."
   }
  ],
  "code": 403,
  "message": "Moving folders into shared drives is not supported."
 }
}

谷歌文档表明,通过适当的权限,可以将文件夹移动到共享驱动器中。如何使用 Google Drive API 来完成?

标签: phpgoogle-drive-apigoogle-drive-shared-drive

解决方案


我认为,就像错误消息所说的那样,它不受支持。文档说这是可能的,但没有说 API 可以做到这一点。历史上,Google API 并不总是公开用户可以执行的所有操作。如果你想用 API 来做,你可能会不走运。

https://developers.google.com/drive/api/v3/about-shareddrives

API 文档根本没有提到将文件移动到共享驱动器中。仅管理共享驱动器的权限。


推荐阅读