Google Drive API文件夹权限

时间:2018-11-11 10:21:25

标签: google-api google-drive-api

我在使用Google Drive API v3时遇到问题。 我以Google为例,并尝试了。我可以读取文件夹内容,但没有获得文件夹权限。

我收到错误403,“权限不足”。 我无法创建或读取该文件夹的所有权限。

<?php
require_once __DIR__.'/vendor/autoload.php';

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setAccessType('offline');

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);
    $drive = new Google_Service_Drive($client);

    $folderId = '1_ip3-WUs4F6atNdElJ3KHccAV4lI0nLL';

    $optParams = array(
        'pageSize' => 100,
        'fields' => "nextPageToken, files(id,name)",
        'q' => "'".$folderId."' in parents"
        );

    $results = $drive->files->listFiles($optParams);

    if (count($results->getFiles()) != 0) {
        foreach ($results->getFiles() as $file) {
            echo "Id: " . $file->getId() . "  Name: " . $file->getName() . "<br>";
        }
    }   

} else {
    $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

//+!+!+!+!+!+!+!+!+!+! Next code doesn't WORK  !+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!//

$fileId = '1UXSg5W-XIX82izGK8uEXXWPUCLcDGRa1';

$userPermission = new Google_Service_Drive_Permission(array(
    'type' => 'user',
    'role' => 'reader',
    'emailAddress' => 'email@gmail.com'
    ));

$request = $drive->permissions->create($fileId, $userPermission, array('fields' => 'id'));
echo $request; 

?>

1 个答案:

答案 0 :(得分:2)

即使您说您什么也不做,可能是在创建文件夹时设置了错误的权限。我在这里看不到用于创建文件夹的代码,只是一个ID,它表示该文件夹已经创建。

我建议先尝试使用Drive.Permissions.List列出权限。特别是,请查看typeroleteamDrivePermissionDetails[].role属性。有关每个角色或类型可以执行的操作,请参见permissions guide。如果需要,您也可以在managing sharing上访问此文档。