php - 如何为 API Drive v3 设置任何人的权限
问题描述
所以故事是这样的,我有一个 Google Drive 文件共享网络,现在当我使用它时,应用程序复制的文件仍然是私有的,我如何更改权限使其成为任何人/公共?
下面的脚本无法为任何人设置文件复制权限。
这是我使用的代码功能:
function insertPermission($id, $token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/$id/permissions?key=$token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json", "Authorization: Bearer $token", "{\"role\":\"reader\",\"type\":\"anyone\"}"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"role\":\"reader\",\"type\":\"anyone\"}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
function copyfile($id, $token) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/$id/copy?access_token=$token");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-length: 0", "Content-type: application/json", "Authorization: Bearer $token"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
这是调用代码:
$ruse = token("refresh", $_SESSION[email]);
$copy = copyfile($share[file][file_id], $ruse[access_token]);
insertPermission($copy[id], $ruse[access_token]);
if ($copy[id]) {
echo " <
meta http - equiv = \"refresh\" content=\"10;URL='https://docs.google.com/uc?id=$copy[id]'\" /> <
div class = \"my-3 p-3 bg-white rounded shadow\"> <
div style = \"padding: 50px 15px;text-align: center;background-color: #fff;\"> <
h3 > Start Download < /h3><br / >
Click < a href = \"https://docs.google.com/uc?id=$copy[id]\" target=\"_blank\">here</a> for download. <
/div> <
/div>";
}
解决方案
回答:
您可以使用 Google Drive API 的Permissions: create
[1]方法使用您需要的参数创建一个新的权限。
更多信息:
使用您希望设置权限的文件的 ID 向 Drive API发出Permissions: create
请求将设置权限。获取到你的应用程序新创建的文件的文件ID后,你可以连同一个包含权限类型的请求体一起发出请求。
示例 cURL 请求:
curl --request POST \
'https://www.googleapis.com/drive/v3/files/<YOUR-FILE-ID-GOES-HERE>/permissions?key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"role":"reader","type":"anyone"}' \
--compressed
该data '{"role":"reader","type":"anyone"}'
行设置权限,以便任何拥有链接的人都可以查看文件,但不能编辑。您可以在 Drive API 文档中阅读有关权限资源[2]type
的更多信息,其中包含和的允许值role
。