0
votes

I have problem with google drive api v3. I took google example and I tried. I could read folder content but I did nothing with folder permission.

I received error 403, "Insufficient Permission". I can't create or read all permissions for the folder.

<?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 protected]'
    ));

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

?>
1

1 Answers

2
votes

It may be that wrong permissions have been set when creating the folder, even though you said you did nothing. I do not see the code for creating the folder here, just an ID, which indicates that the folder has already been created.

I suggest trying to list the permissions using Drive.Permissions.List first. Particularly, look at the type and role or teamDrivePermissionDetails[].role properties. See the permissions guide on what operations each role or type can do. You can also visit this documentation on managing sharing in case you need to.