I currently have code to upload data to a google sheet in google drive. However, I want to give another user access to this google sheet. I tried some code that I found online to give access to another user, included below, but that code gave me an error, also included below.
This is my code:
from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('sheets', 'v4', credentials=creds) #drive_api = build('drive', 'v3', credentials=credentials) # Please set worksheet names. sheetNameForWorksheet1 = "Report Overview" sheetNameForWorksheet2 = "Error Details" sheetNameForWorksheet3 = "% of missing values per column" # Please set values for each worksheet. Values are 2 dimensional array. valuesForWorksheet1 = overview_df.T.reset_index().T.values.tolist() valuesForWorksheet2 = details_df.T.reset_index().T.values.tolist() valuesForWorksheet3 = miss_occ_df.T.reset_index().T.values.tolist() spreadsheet = { 'properties': { 'title': 'Data Integrity Report Completed on {}'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) }, "sheets": [ { "properties": { "title": sheetNameForWorksheet1 } }, { "properties": { "title": sheetNameForWorksheet2 } }, { "properties": { "title": sheetNameForWorksheet3 } } ] } spreadsheet = service.spreadsheets().create(body=spreadsheet, fields='spreadsheetId').execute() gsheet_id = spreadsheet.get('spreadsheetId') batch_update_values_request_body = { "data": [ { "values": valuesForWorksheet1, "range": sheetNameForWorksheet1, }, { "values": valuesForWorksheet2, "range": sheetNameForWorksheet2, }, { "values": valuesForWorksheet3, "range": sheetNameForWorksheet3, } ], "valueInputOption": "USER_ENTERED" } response = service.spreadsheets().values().batchUpdate(spreadsheetId=gsheet_id, body=batch_update_values_request_body).execute() I found this code online and tried it:
drive = build('drive', 'v3') domain_permission = { 'type': 'domain', 'role': 'writer', 'emailAddress': '[email protected]', # Magic almost undocumented variable which makes files appear in your Google Drive 'allowFileDiscovery': True, } req = drive.permissions().create(fileId=gsheet_id, body=domain_permission, fields="id") req.execute() However, this gave me the following error:
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started What can I do to get around this?