PHP: How to share a file publicly using Google Drive Api


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 );

share a file publicly using Google Drive Api

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) {
  }
}

Leave a Reply