2
votes

I have a PHP web application integrated with GoogleDriveAPI v3 which creates folders on Google Drive, sets different permissions to a list of users, shows to each user the folder content and the users should be able to get access to the files (which have the same permissions of the folder) opening them in a new window outside my application, directly in Google Docs. These users might not have a Google Account.

In my database, I store the folder Id got by Google Drive API and the permissions IDs got when the permissions are set.

When users views the folder content, my application calls the related API to get the list of files inside the folder (explained here https://developers.google.com/drive/api/v3/reference/files/list) and I manage the webViewLink json key which is the link of the document, but I don't know how to give the way to open the file, directly in Google Docs, passing the permissions for those users without a Google Account.

I tried to create and share a document outside my application, directly in a Google Drive folder, and I received an email with a link like this: https://docs.google.com/document/d/xxxxxx-file-id/edit?usp=sharing_eip&ts=5d03b1c1 Note: I can't post the right link because of the reserved content. By opening the link I was able to modify the document even if I was not logged in as a Google Account (my nickname is shown as an animal), so someway Google knows that opening the link I can edit the file.

   //create Folder
   public function createRootFolder($name, $folder_root)
   {
        $fileMetadata = new Google_Service_Drive_DriveFile(array(
            'name' => $name,
            "parents" => [$folder_root],
            'mimeType' => 'application/vnd.google-apps.folder'));
             $file = $this->service->files->create($fileMetadata, array(
            'fields' => 'id'));
        return $file;
    }

    //setPermission
    public function setPermission($folderId, $typePermission = 'user' , 
    $rolePermission , $emailAddress)
    {
        $return = false;
        $this->service->getClient()->setUseBatch(true);
        try {
            $batch = $this->service->createBatch();

            $userPermission = new Google_Service_Drive_Permission(array(
                'type' => $typePermission,
                'role' => $rolePermission,
                'emailAddress' => $emailAddress,
                'setDisplayName' => $emailAddress
            ));
            $request = $this->service->permissions->create(
                $folderId, $userPermission, array('fields' => 'id')
            );
            $batch->add($request, 'user');

            $results = $batch->execute();

            foreach ($results as $result) {
                if ($result instanceof Google_Service_Exception) {
                    $return = null;
                } else {
                    $return =  $result->id ;
                }
            }
        } finally {
            $this->service->getClient()->setUseBatch(false);
        }
        return $return;
    }

   //getFileList
    public function getFileList($folderId=null)
    {
        $l = [];
        $optParams = array(
            'fields' => '*',
            'q' => '\''.$folderId.'\' in parents'
        );

        $response = $this->service->files->listFiles($optParams);
        $ll = [];
        foreach ($response->files as $file) {
            array_push($l,
                array(
                    'id'=>$file->id,'name'=>$file->name,
                    'modifiedTime'=>$file->modifiedTime,
                    'createdTime'=> $file->createdTime,
                    'webViewLink' => $file->webViewLink
                ));

            array_push($ll ,array('file', $file));
        }


        return $l;

    }

Here I would like open the Google Docs File

<a href="https://docs.google.com/document/d/xxxxxx-file-id/edit">Apri</a> 

I expect as I can open and modify the file when I receive the link by email, I could generate the same link for users to open it through my application.

1

1 Answers

1
votes

You are talking about a link accessible for everyone with the link, as you said the primary difference between this way and sharing the Google Doc with a specific google account is that if you use the anyoneWithTheLink way, even if you log in with your google account you’re going to be anonymous, meaning the edits you do will appear as anonymous [1].

If you want to obtain this editable link, you would have to use the following $userPermission variable rather than the one you’re using [2]:

$userPermission = new Google_Service_Drive_Permission(array(
                'type' => ’anyone’,
                'role' => ‘writer’,
                ‘allowFileDiscovery’ => false //If true, the link will be discoverable in internet
            ));

You could use the setPermission method once for each file, so they’ll all have the shareable link as editable by anyone with the link.

[1] https://support.google.com/drive/answer/2494822?hl=en&co=GENIE.Platform=Desktop

[2] https://developers.google.com/drive/api/v3/reference/permissions/create