1

How do I change the privacy status of a file using Google Drive Api and Python? I can capture this state with file['shared'] which returns a boolean 0 (private) or 1 (public)

Tobias
  • 13
  • 2
  • In your situation, you are the owner of the file? And, where is the file? For example, it's in your Google Drive or a shared Drive? – Tanaike Feb 12 '22 at 01:44
  • I am the owner of the file and it is in my google drive – Tobias Feb 12 '22 at 02:32
  • Doing this manually is very simple, you just go to +Share and then where it says Get Link you put Change and choose the Restricted option so that it remains private, But I need to do it from python – Tobias Feb 12 '22 at 02:32
  • Thank you for replying. From your replying, I proposed a sample script as an answer. Could you please confirm it? If that was not useful, I apologize. – Tanaike Feb 12 '22 at 03:48
  • The code works perfectly for what I need – Tobias Feb 12 '22 at 04:25

1 Answers1

0

I believe your current situation and your goal are as follows.

  • You want to change shared of a file from true to false using pydrive for python.
  • You have already been able to get and put values for a file using Google Drive API.
  • You are the owner of the file, and the file is put in your Google Drive.

In this case, how about the following sample script?

Sample script:

In this script, all permissions except for the owner are deleted.

ownerEmail = '###'  # Please set the owner's email address.
file_id = '###'  # Please set the file ID.
drive = GoogleDrive(gauth) # Please your your authorization script.

file = drive.CreateFile({'id': file_id})
permission_list = file.GetPermissions()
for obj in permission_list:
    if obj.get('emailAddress') != ownerEmail:
        file.DeletePermission(obj['id'])

Or, if the file is publicly shared and you want to delete only the publicly shared permission, you can use the following script.

file_id = '###'  # Please set the file ID.
drive = GoogleDrive(gauth) # Please your your authorization script.
drive.CreateFile({'id': file_id}).DeletePermission('anyoneWithLink')

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165