spa*_*ode 4 google-drive-api google-api-php-client service-accounts
尝试转移所有权时,出现以下错误:
"domain": "global",
"reason": "invalidSharingRequest",
"message": "Bad Request. User message: \"You can't change the owner of this item.\""
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;
use Google_Service_Drive_Permission;
public function uploadDocument() {
$FOLDER_ID = 'my_folder_ID_string';
$client = new Google_Client();
$client->setAuthConfig(base_path('service_account_credentials.json'));
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Test.pdf',
'parents' => [$FOLDER_ID]
));
$content = file_get_contents(public_path('tmp/Test.pdf'));
$file = $service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'application/pdf',
'uploadType' => 'multipart',
'fields' => 'id'
));
// Transfer Ownership
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setRole('owner');
$newPermission->setType('user');
$newPermission->setEmailAddress('email@gmail.com');
$optParams = array('transferOwnership' => 'true');
$service->permissions->create($file->id, $newPermission, $optParams);
}
Run Code Online (Sandbox Code Playgroud)
该文件夹已成功上传到共享文件夹中的Google Drive(所有者为'email@gmail.com',服务帐户为'editor'),但上传文件的所有者为服务帐户且编辑者为'email@gmail .com'。
根据文档:
共享云端硬盘中的文件归共享云端硬盘所有,而非个人用户。
因此,无需从服务帐户中“夺走”所有权。
您可以赋予用户任何其他角色,例如organizer
允许用户将文件移出共享驱动器,从而成为文件的所有者。\
更新
如果文件是由服务帐户上传到用户云端硬盘上的共享文件夹而不是共享云端硬盘,则情况会有所不同。
具体来说:
Ownership can only be transferred to another user in the same organization as the current owner.
。在 GCP 控制台中为特定服务帐号启用全域委托。
继续,在您的管理控制台中授权服务帐户所需的所有范围Security > API Controls > Domain wide delegation
。
通过添加行来修改您的代码
$client->setSubject($EmailOfUserToImpersonate);
归档时间: |
|
查看次数: |
4860 次 |
最近记录: |