1

I am using Google Drive API in Laravel 5.2. I am able to give access for a file to a particular email ID of Gmail. However, I want to give access publicly for all who ever have the file link.

I have tried this webViewLink, alternateLink and webContentLink, but all these are blank.

See Image output of file object

enter image description here

    if(isset($options['name']) && isset($options['path'])){
        $auth_con = new AuthController();
        $client = $auth_con->getClient();
        $service = new \Google_Service_Drive($client);
        $fileMetadata = new \Google_Service_Drive_DriveFile(array(
            'name' => $options['name']));
        $content = file_get_contents($options['path']);
        $file = $service->files->create($fileMetadata, array(
            'data' => $content,
            'uploadType' => 'multipart',
            'fields' => 'id'
        ));
        $file_id = $file->id;
        if(isset($options['to'])){
            foreach ($options['to'] as $key => $value) {
                drive_permission($service, $value, $file_id);
            }
        }
     }


    function drive_permission($service, $email_id, $file_id)
    {
      $service->getClient()->setUseBatch(true);
      try {
        $batch = $service->createBatch();

        $userPermission = new Google_Service_Drive_Permission(array(
            'type' => 'group',
            'role' => 'reader',
            'emailAddress' => $email_id
        ));
        $request = $service->permissions->create(
            $file_id, $userPermission, array('fields' => 'id'));
        $batch->add($request, 'anyone');
        $results = $batch->execute();
      } finally {
        $service->getClient()->setUseBatch(false);
    }
}
droidBomb
  • 850
  • 5
  • 8
Sohail Ansari
  • 350
  • 4
  • 10

1 Answers1

0

For Google Drive v2, use the alternateLink property. While in Google Drive v3, use the webViewLink property.

Also as per this link:

In my case using the PHP Api v3, for the link to be non-empty you must define that you request this field, and if you have the right permissions:

so something like this:

$file =self::$service->files->get("1ogXyGxcJdMXt7nJddTpVqwd6_G8Hd5sUfq4b4cxvotest",array("fields"=>"webViewLink"));
abielita
  • 13,147
  • 2
  • 17
  • 59
  • 1
    thanks but it asks for login through Gmail and I have tried this before. I want to give the public access to whoever has this link. – Sohail Ansari Oct 15 '18 at 09:33