From b999fd0fecf4b209c39785bd9d2be6990ee1716e Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 22 Oct 2020 21:40:36 -0700 Subject: [PATCH] After creating spreadsheets, make them writable by anyone with a link This requires a new scope for our credentials, and a new service. But with those in place, it's just a single API call. --- gsheets-authenticate.py | 5 ++++- turbot/sheets.py | 6 ++++++ turbot_lambda/turbot_lambda.py | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gsheets-authenticate.py b/gsheets-authenticate.py index 2d0a609..ca0d0fa 100755 --- a/gsheets-authenticate.py +++ b/gsheets-authenticate.py @@ -9,7 +9,10 @@ from google.auth.transport.requests import Request TOKEN_FILE = ".gsheets-pickle.base64" # If modifying these scopes, delete the token file -SCOPES = ['https://www.googleapis.com/auth/spreadsheets'] +SCOPES = [ + 'https://www.googleapis.com/auth/spreadsheets', + 'https://www.googleapis.com/auth/drive' +] def main(): """Allows user to authenticate for the Sheets API. diff --git a/turbot/sheets.py b/turbot/sheets.py index 8ab302d..f2a9681 100644 --- a/turbot/sheets.py +++ b/turbot/sheets.py @@ -16,6 +16,12 @@ def sheets_create(turb, name): new_sheet = turb.sheets.create(body=spreadsheet_body).execute() + # Now that we've created a new sheet, we need to also allow anyone + # with the link to the sheet to be able to edit it. + turb.permissions.create(fileId=new_sheet["spreadsheetId"], + body={'type': 'anyone', 'role': 'writer'}, + fields='id').execute() + return { 'id': new_sheet["spreadsheetId"], 'url': new_sheet["spreadsheetUrl"] diff --git a/turbot_lambda/turbot_lambda.py b/turbot_lambda/turbot_lambda.py index e19e96d..4074d77 100644 --- a/turbot_lambda/turbot_lambda.py +++ b/turbot_lambda/turbot_lambda.py @@ -43,6 +43,11 @@ service = build('sheets', credentials=gsheets_creds, cache_discovery=False) sheets = service.spreadsheets() +service = build('drive', + 'v3', + credentials=gsheets_creds, + cache_discovery=False) +permissions = service.permissions() db = boto3.resource('dynamodb') @@ -50,6 +55,7 @@ turb = SimpleNamespace() turb.slack_client = slack_client turb.db = db turb.sheets = sheets +turb.permissions = permissions def error(message): """Generate an error response for a Slack request -- 2.43.0