2

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?

1 Answer 1

1
  • You want to create a permission for the Spreadsheet.
  • You want to achieve them using google-api-python-client with Python.

If my understanding is correct, how about this answer?

Issue:

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

The reason of above error message is drive = build('drive', 'v3'). In this case, the access token for using the API is not included. So please modify as follows.

Modified script:

From:
drive = build('drive', 'v3') 
To:
drive = build('drive', 'v3', credentials=creds) 

Note:

  • In your situation, when the modified script is run, if the error of Request had insufficient authentication scopes. occurs, please do the following flow.
    1. Please add https://www.googleapis.com/auth/drive to SCOPE like SCOPES = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"].
    2. Please delete the file of token.pickle and authorize the scopes by running the script.
      • By this, the additional scope is reflected to the access token.
  • This modified script supposes the settings of object of domain_permission are correct.

    • If you want to permit to edit as an user, please try the following object.

      domain_permission = { 'type': 'user', 'role': 'writer', 'emailAddress': '[email protected]' } 

Reference:

If I misunderstood your question and this was not the result you want, I apologize. If other error occurs, can you show the error message? And also can you provide your whole script? By this, I would like to confirm it. Of course, please remove your personal information.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! Thanks again!
@user11371534 Welcome. I'm glad your issue was resolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.