With Google Drive, there are two levels of share publicly a file:
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('0ByRMI9N7giJ-SW1iV1ZIbTh1U0E', $newPermission );
The result:
object(Google_Service_Drive_Permission)#31 (18) { ["collection_key":protected]=> string(15) "additionalRoles" ["internal_gapi_mappings":protected]=> array(0) { } ["additionalRoles"]=> NULL ["authKey"]=> NULL ["domain"]=> NULL ["emailAddress"]=> NULL ["etag"]=> string(57) ""TEeH8_qJkyJwjzQ-F4FJRbfwWxI/1ck4kysqm6M45KwcxcAKG5Yn2x0"" ["id"]=> string(14) "anyoneWithLink" ["kind"]=> string(16) "drive#permission" ["name"]=> NULL ["photoLink"]=> NULL ["role"]=> string(6) "reader" ["selfLink"]=> string(97) "https://www.googleapis.com/drive/v2/files/0ByRMI9N7giJ-SW1iV1ZIbTh1U0E/permissions/anyoneWithLink" ["type"]=> string(6) "anyone" ["value"]=> NULL ["withLink"]=> bool(true) ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } }
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('0ByRMI9N7giJ-SW1iV1ZIbTh1U0E', $newPermission );
Result:
object(Google_Service_Drive_Permission)#31 (18) { ["collection_key":protected]=> string(15) "additionalRoles" ["internal_gapi_mappings":protected]=> array(0) { } ["additionalRoles"]=> NULL ["authKey"]=> NULL ["domain"]=> NULL ["emailAddress"]=> NULL ["etag"]=> string(57) ""TEeH8_qJkyJwjzQ-F4FJRbfwWxI/sRsgZuG6M5n1c5JnayIV5qzc_wk"" ["id"]=> string(6) "anyone" ["kind"]=> string(16) "drive#permission" ["name"]=> NULL ["photoLink"]=> NULL ["role"]=> string(6) "reader" ["selfLink"]=> string(89) "https://www.googleapis.com/drive/v2/files/0ByRMI9N7giJ-SW1iV1ZIbTh1U0E/permissions/anyone" ["type"]=> string(6) "anyone" ["value"]=> NULL ["withLink"]=> NULL ["modelData":protected]=> array(0) { } ["processed":protected]=> array(0) { } }
Update 2/17/2025
Now there is only one level (Level: Anyone with the link) and the new code is:
$newPermission = new Google_Service_Drive_Permission(); $newPermission->setType('anyone'); $newPermission->setRole('reader'); $permission = $service->permissions->create('0ByRMI9N7giJ-SW1iV1ZIbTh1U0E', $newPermission );
Google removed two methods: setValue
and setWithLink
and change method insert
to create
Bonus: share with a domain
$newPermission = new Google\Service\Drive\Permission(); $newPermission->setDomain($domain); $newPermission->setType('domain'); $newPermission->setRole('reader'); $permission = $service->permissions->create($id, $newPermission, array('sendNotificationEmails' => false));