我正在尝试使用 OAuth2 和 gspread 来使用 Python 来操作 Google 表格。但是,我一直遇到这个错误:
invalid_scope: https://spreadsheets.google.com/feeds is not a valid audience string.
最初,我将范围作为列表,但是,我在 StackOverflow 上看到另一个答案,说范围应该是一个空格分隔的字符串。我试着改过来,但错误仍然发生。
这是我的代码:
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())`
最佳答案
错误非常明显:spreadsheets.google.com/feeds
不是 API 范围。
如果您想请求访问 Google 表格文件的权限,请使用表格 REST API 文档中列出的一个或多个 API 范围: https://developers.google.com/sheets/api/guides/authorizing#OAuth2Authorizing
这些都很好地制成了表格:
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.
关于python - invalid_scope : https://spreadsheets. google.com/feeds 不是有效的受众字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49829002/