'Google Picker API - showing only files user can share
I would like to use the Google Picker to choose a specific file from Drive, get its id and then share it with some other accounts using Drive API. The problem is that I can either:
- see all of the files user has access to (even as a reader without sharing permissions),
- see only the files where user is the
owner
.
The first option could obviously cause some issues, as sharing won't always work. The second one could hide some files that can in fact still be shared by the user (usually editors
have the rights to share as well).
Is anyone aware of a way to show only shareable files in the Google Picker?
Thanks, Kacper
Solution 1:[1]
As far as I can tell, this is not possible with the Google Picker API, but the Drive API may help.
I went over the Google Picker documentation. As you may have seen already, the picker is created with PickerBuilder()
, which takes View
objects to determine which files will be visible. The DocsView
subclass has a setOwnedByMe()
method to only show or exclude files that you own, however, this does not check for editor status, and there are no other methods that could do it.
View
does have a setQuery()
method, which seemed promising to add a search query, but I tested it and it uses the Drive UI advanced search syntax, which does not have a way to check for editors either. In fact, setQuery()
only prepopulates the search field already included in the picker.
As an alternative, since you already plan to use the Drive API you could take the selected file IDs from the picker's Response.DOCUMENTS
and then plug them in to files.get
to first validate whether or not the user has editor access and show a warning before proceeding so they will at least know which files cannot be shared.
Also, files.list
has a search guide that you can check out, and the q
parameter does allow searching for editor status with the query '[email protected]' in writers
. If you really need to only show these files you could write an app that uses only the Drive API instead of Google Picker. PickerBuilder()
generates a live object connected to Drive so there is no easy way to plug in the files.list
data to it.
Solution 2:[2]
I have exact same scenario as you. Unfortunately I dont find any existing configuration of Google Picker support it.
Here is the workaround i did:
- Get the selected document
- Call drive.permissions.list, loop through and few conditions to check
- type:user and email:<the user email, you need email> and role:editor OR
- type:anyone and role:editor
Reference: https://developers.google.com/drive/api/v3/reference/permissions#resource
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel |
Solution 2 | CodeMonkey |