pythongoogle-drive-apipydrive

Change the Privacy Status of a file using Google Drive Api and Python


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)


Solution

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

    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: