pythongoogle-drive-apigoogle-drive-shared-drive

How to add organization access to Google Drive folder?


How do you programmatically grant access to everyone in an organization to a shared folder on Google Drive using the v3 Drive Python API?

These docs clearly explain how to grant access to a specific user, but this doesn't seem to work for granting your entire group (e.g. "Anyone in this group with the link can view") option under the "General access" section.

This is what I've tried:

from apiclient import discovery

credentials = get_google_drive_credentials()
http = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http=http)

body = {
    'name': 'My Folder',
    'mimeType': "application/vnd.google-apps.folder",
    'supportsAllDrives': True,
    'allowFileDiscovery': True,
    'type': 'domain',
    'domain': 'MyDomain',
}
new_folder = drive_service.files().create(body = body).execute()

And while it create the folder successfully, when I view the new folder on Google Drive, it still shows at as "My Drive", owned by me, and shared with no one.

Why is it ignoring the domain type and not sharing it with everyone in my group?


Solution

  • In your showing script, how about the following modification?

    Modified script:

    Please set your domain to ### of "domain": "###".

    from apiclient import discovery
    
    credentials = get_google_drive_credentials()
    http = credentials.authorize(httplib2.Http())
    drive_service = discovery.build('drive', 'v3', http=http)
    
    # Create folder
    body = {
        'name': 'My Folder',
        'mimeType': "application/vnd.google-apps.folder",
    }
    new_folder = drive_service.files().create(body=body, supportsAllDrives=True).execute()
    
    # Create permission
    res = drive_service.permissions().create(
        body={
            "role": "writer",
            "type": "domain",
            "allowFileDiscovery": True,
            "domain": "###"  # Please set your domain
        }, fileId=new_folder["id"], supportsAllDrives=True, fields='*').execute()
    print(res)
    

    Note:

    References: