Search code examples
phpgoogle-drive-api

How to create a public accessible folder in google drive through drive API?


I am creating a folder in Google Drive to upload images in it, but to access those images without any authentication I want to make folder public at the time of creating it.

But unfortunately I have not found any additional function or argument to use with this API to create a new public folder with public accessible permission.

Here is my code for creating a new folder in Drive:

$folder = new Google\Service\Drive\DriveFile();
$folder->setName("MyPublicFolder_media");
$folder->setMimeType('application/vnd.google-apps.folder');
$createdFolder = $service->files->create($folder);
$folderId = $createdFolder->getId(); 

Solution

  • Inside the Google Documentation shared by @ADyson, you can find a sample on how to share the file using the APIs, just change 'type': 'anyone'.

    Also, I found these sample codes.

    Level: Anyone with the link

    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setValue('default');
    $newPermission->setType('anyone');
    $newPermission->setRole('reader');
    $newPermission->setWithLink('true');
     
    $permission = $service->permissions->insert('File_ID', $newPermission );
    

    Level: Public on the web – Anyone can find and view

    $newPermission = new Google_Service_Drive_Permission();
    $newPermission->setValue('default');
    $newPermission->setType('anyone');
    $newPermission->setRole('reader');
      
    $permission = $service->permissions->insert('File_ID', $newPermission );
    

    Modified from this post