3

I'm trying to use OAuth2 and gspread to use Python to manipulate Google Sheets. However, I keep running into this error:

invalid_scope: https://spreadsheets.google.com/feeds is not a valid audience string.

Initially, I had the scope as a list, however, I saw another answer on StackOverflow saying that the scope should be a space delimited string instead. I tried changing it over but the error still occured.

Here is my code:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ' '.join(['https://spreadsheets.google.com/feeds', 'https://googleapis.com/auth/drive'])

credentials = ServiceAccountCredentials.from_json_keyfile_name("SpreadsheetExample-bec536232207.json", 'https://spreadsheets.google.com/feeds https://googleapis.com/auth/drive')

gc = gspread.authorize(credentials)
wks = gc.open("test").sheet1

print(wks.get_all_records())`
Rattleznake
  • 31
  • 1
  • 3

1 Answers1

4

The error is very clear: spreadsheets.google.com/feeds is not an API scope.

If you want to request permission to access Google Sheets files, use one or more of the API scopes listed in the Sheets REST API documentation: https://developers.google.com/sheets/api/guides/authorizing#OAuth2Authorizing

These are tabulated nicely there:

Scope Meaning
https://www.googleapis.com/auth/spreadsheets.readonly Allows read-only access to the user's sheets and their properties.
https://www.googleapis.com/auth/spreadsheets Allows read/write access to the user's sheets and their properties.
https://www.googleapis.com/auth/drive.readonly Allows read-only access to the user's file metadata and file content.
https://www.googleapis.com/auth/drive.file Per-file access to files created or opened by the app.
https://www.googleapis.com/auth/drive Full, permissive scope to access all of a user's files. Request this scope only when it is strictly necessary.

tehhowch
  • 9,645
  • 4
  • 24
  • 42