I’m trying to follow the Google Sheet API from its documentation here:
https://developers.google.com/sheets/api/guides/values
I’m trying to run the snippets of the codes mentioned there, for example, Python code on Reading a single range
Here is an example of a snippet of code that I ran, sheets_get_values.py
from __future__ import print_function
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def get_values(spreadsheet_id, range_name):
creds, _ = google.auth.default()
try:
service = build('sheets', 'v4', credentials=creds)
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id, range=range_name).execute()
rows = result.get('values', [])
print(f"{len(rows)} rows retrieved")
return result
except HttpError as error:
print(f"An error occurred: {error}")
return error
if __name__ == '__main__':
get_values("1CM29gwKIzeXsAppeNwrc8lbYaVMmUclprLuLYuHog4k", "A1:C2")
python3 sheets_get_values.py
But I got this message:
An error occurred: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1CM29gwKIzeXsAppeNwrc8lbYaVMmUclprLuLYuHog4k/values/A1%3AC2?alt=json returned "Request had insufficient authentication scopes.". Details: "[{'@type': 'type.googleapis.com/google.rpc.ErrorInfo', 'reason': 'ACCESS_TOKEN_SCOPE_INSUFFICIENT', 'domain': 'googleapis.com', 'metadata': {'method': 'google.apps.sheets.v4.SpreadsheetsService.GetValues', 'service': 'sheets.googleapis.com'}}]">
I have tried to set the GOOGLE_APPLICATION_CREDENTIALS environment:
export GOOGLE_APPLICATION_CREDENTIALS='/home/myUser/.config/gcloud/application_default_credentials.json'
I also already enable application default credentials:
gcloud auth application-default login
gcloud config set project project_name
I also try to solve this by following a few instructions related to Request had insufficient authentication scopes by Googling, for example this one
I try to modify the default snippet of code by Google documentation be like this:
SCOPES = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/spreadsheets',
]
def append_values(spreadsheet_id, range_name, value_input_option,
_values):
creds, _ = google.auth.default(scopes=SCOPES)
...
...
But the error still appear when I ran the snippet of the code.
So, what should I do to solve this error?
2
Answers
The problem is in the Sheet you are trying to get data from. It is unavailable, you can try to navigate to it.
Replace the sheet id (
1CM29gwKIzeXsAppeNwrc8lbYaVMmUclprLuLYuHog4k
) with some that is available to your Google account. You can try this one –1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms
(this is the test sheet).Instead of:
Try passing in additional scopes:
You may also need to unset
GOOGLE_APPLICATION_CREDENTIALS
beforeapplication-default login
works.