使用 Google Drive API 變更透過帳戶服務建立的文件的擁有者
P粉680487967
P粉680487967 2024-01-06 11:03:37
0
1
491

我使用服務帳戶將文件上傳到 Google 雲端硬碟中的共用資料夾。

一段時間後,我發現服務帳戶擁有的檔案消耗了服務帳戶的磁碟機儲存(我的錯),現在我已經用完了服務帳戶的磁碟機空間。

我已經將網域範圍的權限委託給服務帳戶,因此新文件將歸我所有並使用我的個人儲存配額。

這樣做:將網域範圍的權限委派給服務帳戶

以及如何透過 google/apiclient 從 PHP 使用 Google Drive API 的 API 金鑰

為了避免將來出現錯誤和混亂,我想更改舊文件的擁有者。我不斷收到此錯誤:

{ "error": { 
    "code": 400, 
    "message": "Bad Request. User message: "You can't change the owner of this item."", 
    "errors": [ {
       "message": "Bad Request. User message: "You can't change the owner of this item."", 
       "domain": "global", 
       "reason": "invalidSharingRequest" 
    } ] 
  } 
}

這是我使用 PHP 客戶端的程式碼

$client = new Google_Client();
$client->setApplicationName('My Name');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig($my_credentials);
$client->setAccessType('offline');
//$client->setSubject('my_personal_account');

$service = new Google_Service_Drive($client);

$newPermission = new Google_Service_Drive_Permission();
$newPermission->setEmailAddress('my_personal_account');
$newPermission->setType('user');
$newPermission->setRole('owner');

$service->permissions->create(
  $fileId, 
  $newPermission, 
  array("fields" => "id", "transferOwnership" => true)
);

無論客戶端是否使用 setSubject,我都會遇到相同的錯誤。我嘗試過使用

$newPermission->setRole('writer');
$newPermission->setPendingOwner(true);

但是沒有成功。

P粉680487967
P粉680487967

全部回覆(1)
P粉571233520

只能在同一網域的帳戶之間轉移檔案所有權。出現此錯誤的原因是服務帳戶和您的帳戶不屬於相同網域。

如果您有權存取共用雲端硬碟並且能夠新增具有新增檔案權限的用戶,請新增服務帳戶並使其將檔案移至共用雲端硬碟。

相關

其他相關

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!