pythongoogle-drive-api

Changing access option of a file using Google Drive API


Is it possible to change the access option of a file in Drive from 'Restricted' to 'Anyone with the Link' using Google Drive API?

filename= 'filename' file_metadata = { 'name' : filename, 'parents' : [PARENT_FOLDER_ID], 'type':'anyone', 'role':'reader' }

I have tried tweaking the 'type' parameter however that doesn't seem to be related to the issue.


Solution

  • Yes, you can change the access option of a file from 'Restricted' to 'Anyone with the Link' using the Google Drive API. You'll need to use the permissions endpoint to modify file sharing settings.# Create permission for "Anyone with the link"

    # Create permission for "Anyone with the link"
    permission = {
        'type': 'anyone',  # Allows access to anyone
        'role': 'reader',  # Gives read access
        'allowFileDiscovery': False  # Makes it accessible only via link
    }
    
    # Create the permission
    service.permissions().create(
        fileId=file_id,
        body=permission,
        fields='id'
    ).execute()
    

    If you want to update an existing permission instead of creating a new one, you can use the permissions.update endpoint. https://developers.google.com/drive/api/reference/rest/v3/permissions/update