Filip
Filip

Reputation: 605

Authenticate to Gsuite APIs without service account

I'm trying to read Google Spreadsheet using my local credentials (i.e. not service account). I'm basically doing:

from googleapiclient.discovery import build

service = build('sheets', 'v4')
request = sheet_service.spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID, range='{}!A1:a4'.format(SHEET_NAME))
result = request.execute()
values = result.get('values', [])

However I get the following error:

Traceback (most recent call last):
  File "./update_incidents_tracker.py", line 54, in <module>
    sys.exit(main())
  File "./update_incidents_tracker.py", line 48, in main
    result = request.execute()
  File "/home/filip/.virtualenvs/monitoring-tools/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/filip/.virtualenvs/monitoring-tools/lib/python3.6/site-packages/googleapiclient/http.py", line 898, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets/1lTcQ3WknG_2oZvy9O9LEbgAzoykOWaheYSaDkkV21wE/values/Incidents%21A1%3Aa4?alt=json returned "Request had insufficient authentication scopes.">

I can't find a way to set scopes for my local (gcloud SDK) credentials. How can I set proper scopes?

For reference for service account based authentication I can simply run:

credentials = service_account.Credentials.from_service_account_file(CREDENTIALS_FILE, scopes=SCOPES)
build('sheets', 'v4', credentials=credentials)

Upvotes: 0

Views: 105

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75715

Use the default credential explicitally, and set the scope at this time

import google.auth

credentials, project_id = google.auth.default(scopes=....)
build('sheets', 'v4', credentials=credentials)
....

Full documentation of google-auth here

Upvotes: 0

Related Questions