我上传文件到谷歌驱动器帐户,我想做的是使其可读,但不能向公众编辑。我怎样才能做到这一点?
$file = new Google_Service_Drive_DriveFile();
$file->title = $_POST['name'];
$file->shared = "public"; //i need to do this
$file->folder = "/somefolder/"; // and this
[可选的]
它实际上是三合一问题:我还想将该文件放入驱动器上的特定文件夹中,我想知道如何在谷歌驱动器上创建该文件夹。
可能对新手有用:
<?php
$client = new Google_Client();
// SET Client ID, Client Secret, Tokens as per your mechanism
?>
步骤1:
第2步:
使用setParents([]);
将文件上传到该文件夹
例:
<?php
// Upload the file to efgh folder which is inside abcd folder which is in the root folder
$file = new Google_Service_Drive_DriveFile();
$file->setParents(["abcd123", "efgh456"]); // abcd123 and efgh456 are IDs returned by API for the respective folders
$service = new Google_Service_Drive($client);
$service->files->create($file);
?>
$fileID = ""; // file ID returned by Drive.
第3步:
将权限设置为可共享:
<?php
$permissionService = new Google_Service_Drive_Permission();
$permissionService->role = "reader";
$permissionService->type = "anyone"; // anyone with the link can view the file
$service->permissions->create($fileID, $permissionService);
?>
参考:https://developers.google.com/drive/api/v3/reference/permissions/create