I have a Python script that parses an HTML file and puts the information I need from it into a Google Sheets doc that is created by the service account and shared with me. Everything works fine EXCEPT for that the resulting service-account-owned spreadsheet is shared not just with me, but with everyone in my G Suite organization. Not a huge deal since my organization is very small, but I'm sure my coworkers don't want these files cluttering up their "Shared with me" folder.
I've web-searched the heck out of this and perused Stack Overflow but haven't come up with any ideas for changes to my code to try. I also wondered if it might be a setting I need to change in the Google API Console, but haven't found anything there either.
Here's the relevant code:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def loadTemplate(template_name, output_name):
# use creds to create a client to interact with the Google Drive API
print("Connecting to Google API...")
scope = ['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Make a copy of the template and name it something that uniquely identifies this report
print("Copying template...")
open_spreadsheet = client.open(template_name)
copy_spreadsheet = client.copy(open_spreadsheet.id,title=output_name, copy_permissions=True)
copy_spreadsheet.share('myname@myorganization.com', perm_type='user', role='writer', notify=False)
return copy_spreadsheet
template_spreadsheet = loadTemplate(template_name, output_name)
Again, everything runs as expected, except the result is shared with all users in my G Suite organization instead of just with me (myname@myorganization.com). Any suggestions are appreciated!
I figured it out, and I feel pretty stupid...
I was copying a template, and set copy_permissions=True
. The template was not in fact shared with my whole organization, but it was shared with the two people I happened to check with, and as a result every copy of it was by default shared with the same two people. Changing to copy_permissions=False
fixed the issue.