0

I want to use Google Drive API to monitor documents within our organization and make sure that nobody is sharing files improperly. In order to do so, I need to be able to see who (or what groups) currently have access to a document in Drive. I thought that the Drive API would be perfect for this, but when I look up a file in the API the info is totally unhelpful.

Each file has a list of permissions, but when I request this list it doesn't say WHO each permission applies to. They say the role (i.e. "reader", "owner", etc) and the type (i.e. "user", "group", etc), but the email_address field is always blank. Even when I'm the owner of the file, or the permission applies to me.

Am I doing something wrong? Is there any way that I can see who the file permissions apply to? Or am I out of luck?

Here is a condensed version of the PHP code that I'm using:

/* getClient creates and returns a Google_Client */
$client = $this->getClient();
$service = new Google_Service_Drive($client);

/* I use the service to look at my files and get a file id */
$optParams = array(
    'pageSize' => 20,
    'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
foreach ($results->getFiles() as $file) {
    $permissions = $service->permissions->listPermissions($file->getId());
    var_dump($permissions);
}

P.S. This answer suggests that email addresses are left out for privacy reasons (and also seems to imply that they're never visible?) But it's also talking about V2 of the API and so I don't know if it applies. Also I don't understand why it would hide the emails addresses for privacy reasons when I can clearly see them while using Google Drive in browser.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Noah K
  • 77
  • 1
  • 8
  • 2
    Please edit your question and include your code. Try making your request using fields=* parameter. Without seeing your code I cant tell you how to do that. – Linda Lawton - DaImTo Aug 12 '20 at 05:56
  • That was exactly my problem, I totally forgot the fields parameter. Included it and now it works. Thanks. – Noah K Aug 12 '20 at 16:22

1 Answers1

1

As DalmTo pointed out, I just needed to specify my fields in the request. All I did was change:

$permissions = $service->permissions->listPermissions($file->getId());

into:

$optParams = array(
    'pageSize' => 20,
    'fields' => 'nextPageToken, permissions(id, emailAddress, role, type)'
);
$permissions = $service->permissions->listPermissions($file->getId(), $optParams);

and now I can see the email addresses as expected.

Noah K
  • 77
  • 1
  • 8
  • Drive v3 has a drawback (IMO) that it doesnt return all the fields by default you need to request them yourself. [fields parameter](https://developers.google.com/drive/api/v3/fields-parameter) – Linda Lawton - DaImTo Aug 13 '20 at 08:21