Use Google Drive API to change link sharing permis

2020-04-19 05:39发布

问题:

Is there a way to programmatically change the link sharing permissions for a file? I'm administering a Google Apps for Work account and I want to have a policy for every file in a folder named "Confidential" to have a certain set of permissions for Link Sharing, "Prevent editors from changing access and adding new people", and "Disable options to download, print, and copy for commenters and viewers".

It doesn't look like there's a way to do this in the Google Admin console, but is there a way for me to write a script that goes through periodically and "fix" every file's permissions?

回答1:

First of all Google Drive File's uniqueness is in their ID which is given by Google when you upload, not in the filename like the desktop OS.

If you make sure that there is only one "Confidential" file, then you can list files from folder. Later , you can modify "File" properties by :

  1. To "Prevent editors from changing access and adding new people" :

    new File().setWritersCanShare(Boolean value)

  2. To "Disable options to download, print, and copy for commenters and viewers":

    new Labels().setRestricted(Boolean value)

Note that label can only be applied to files not folder. This is using Java, so you can can have a cron to accomplish this task.



回答2:

With the latest version of Google Drive V3, you need to do the following to "Disable options to download, print, and copy for commenters and viewers":

file.setViewersCanCopyContent(false);