]> git.cworth.org Git - turbot/commitdiff
After creating spreadsheets, make them writable by anyone with a link
authorCarl Worth <cworth@cworth.org>
Fri, 23 Oct 2020 04:40:36 +0000 (21:40 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 23 Oct 2020 13:26:19 +0000 (06:26 -0700)
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
turbot/sheets.py
turbot_lambda/turbot_lambda.py

index 2d0a60984ab528c58e571b7fa494e39fa8860c47..ca0d0fa10d7182a774e45331fa6a058fb85a4234 100755 (executable)
@@ -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.
index 8ab302d38579a8f744a475a34a39c769f715813a..f2a9681f7c7483593d6cfad973076dadf802c61e 100644 (file)
@@ -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"]
index e19e96d801ab0b0ea7d7f502e2c66fa341883b1b..4074d7732d3fee7d20aa49b402e682aab2aa9011 100644 (file)
@@ -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