Search code examples
pythongoogle-drive-apigspread

How to use Google Drive v3 API via Python API to restrict editors from sharing a spreadsheet?


I'm trying to figure out how to programmatically in Python implement the solution described here for Prevent others from sharing your shared files (using Google Drive API v3).

I programmatically create a Google spreadsheet as follows:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient import discovery

get_credentials = ServiceAccountCredentials.from_json_keyfile_dict
credentials = get_creds(keyfile_dict=keyfile_dict, scopes=scopes)

service_sheets = discovery.build('sheets', 'v4', credentials=credentials)
service_drive = discovery.build('drive', 'v3', credentials=credentials)

# create sheet
body = {'properties': {'title': 'this is my spreadsheet title'}}
response_create = service_sheets.spreadsheets().create(body=body).execute()

But this spreadsheet is owned by my developer account and I can't edit or even view it. I then want to give myself write permission, which I do as follows:

# change permission
new_permission_w = {
    'value': 'MY_GMAIL_NAME@gmail.com',
    'emailAddress': 'MY_GMAIL_NAME@gmail.com',
    'type': 'user',
    'role': 'writer'
}
perms = service_drive.permissions()
response_perm = (perms
                 .create(fileId=response_create['spreadsheetId'],
                         body=new_permission_w,
                         sendNotificationEmail=False,
                         useDomainAdminAccess=False,
                         transferOwnership=False)
                 .execute())

I want to prevent people I share it with from being able to share it with other people. It looks like there's a way to do this via the API, as indicated here: Use Google Drive API to change link sharing permissions but I can't see how to do it in Python. I tried changing the perms dict to

new_permission_w = {
    'value': 'MY_GMAIL_NAME@gmail.com',
    'emailAddress': 'MY_GMAIL_NAME@gmail.com',
    'type': 'user',
    'role': 'writer',
    'setWritersCanShare': False
}

(i.e., adding 'setWritersCanShare': False to the dict) but the API call seems to ignore the 'setWritersCanShare' key. I also tried changing the API call to

response_perm = (perms
                 .create(fileId=response_create['spreadsheetId'],
                         body=new_permission_w,
                         sendNotificationEmail=False,
                         useDomainAdminAccess=False,
                         setWritersCanShare=False,
                         transferOwnership=False)
                 .execute())

but I get TypeError: Got an unexpected keyword argument "setWritersCanShare".

I also looked to see if there was a gpsread solution for this, but couldn't find one.

I'm at a loss. How can I use the API via python to programmatically prevent editors from sharing?


Solution

    • You want to modify the property of writersCanShare to False using Python.
    • You can use Drive API.
    • Owner of the file you want to modify the property is you.

    If my understanding is correct, how about using the method of files.update in Drive API?

    Sample script:

    body = {'writersCanShare': False}
    res = service_drive.files().update(fileId=response_create['spreadsheetId'], body=body).execute()
    

    Reference:

    If I misunderstood your question, I apologize.