joeb
joeb

Reputation: 877

Copy file with Permissions in Google Drive API PHP

I'm trying to copy a file with a service account, and then grant access to my personal account. The copy seems to be working correctly which it seems to be copying the file to the google drive service account. So it's returning and ID that was created, but it fails when trying to insert the permissions on the file. says undefined method insert.

Here's what I have now

private function copy_base_file( $new_file_name )
{
    $service = $this->get_google_service_drive( $this->get_google_client() );
    $origin_file_id = "{id of file to copy}";
    $copiedFile = new Google_Service_Drive_DriveFile();
    $copiedFile->setName($new_file_name);
    try {
        $response = $service->files->copy($origin_file_id, $copiedFile);
        $ownerPermission = new Google_Service_Drive_Permission();
        $ownerPermission->setEmailAddress("{myemailhere}");
        $ownerPermission->setType('user');
        $ownerPermission->setRole('owner');
        $service->permissions->insert("{sheet_id_here}", $ownerPermission, 
            ['emailMessage' => 'You added a file to ' .
            static::$applicationName . ': ' . "Does this work"]);
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

Upvotes: 1

Views: 2816

Answers (1)

pckaw600
pckaw600

Reputation: 41

The insert method is deprecated in the latest version (V3) of the Google Drive API.

Use the create method instead.

V3 Drive API Permissions Create

private function copy_base_file( $new_file_name )
{
$service = $this->get_google_service_drive( $this->get_google_client() );
$origin_file_id = "{id of file to copy}";
$copiedFile = new Google_Service_Drive_DriveFile();
$copiedFile->setName($new_file_name);
try {
    $response = $service->files->copy($origin_file_id, $copiedFile);
    $ownerPermission = new Google_Service_Drive_Permission();
    $ownerPermission->setEmailAddress("{myemailhere}");
    $ownerPermission->setType('user');
    $ownerPermission->setRole('owner');
    $service->permissions->create("{sheet_id_here}", $ownerPermission, 
        ['emailMessage' => 'You added a file to ' .
        static::$applicationName . ': ' . "Does this work"]);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}

}

Upvotes: 2

Related Questions