【发布时间】:2020-03-09 02:16:54
【问题描述】:
我正在运行这段代码(完全相同)。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Sheet1").sheet1
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
我不完全确定这条线是否正确:
scope = ['https://spreadsheets.google.com/feeds']
无论如何,我按照此链接中的说明进行操作。
我点击了“我的项目”>“服务帐户”>“启用”。我下载了 JSON 文件,将其命名为“client_secret.json”并将其放在此目录中:“C:\Users\ryans\client_secret.json”。最后,我打开 json 文件,在 client_secret.json 中获取“client_email”并将其放入“共享”并点击“保存”按钮。现在,当我运行上面的脚本时,我收到以下错误消息:
Traceback (most recent call last):
File "<ipython-input-20-870ca6cceea6>", line 12, in <module>
sheet = client.open("Sheet1").sheet1
File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 123, in open
self.list_spreadsheet_files()
File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 96, in list_spreadsheet_files
res = self.request('get', url, params=params).json()
File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 79, in request
raise APIError(response)
APIError: {'errors': [{'domain': 'global', 'reason': 'insufficientPermissions', 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}], 'code': 403, 'message': 'Insufficient Permission: Request had insufficient authentication scopes.'}
我不确定这里出了什么问题。会不会是权限问题?
更新
我添加了这一行:
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']
我的 Google 表格如下所示:
现在,我将代码更改为:
import os
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# make sure the 'client_secret.json' is getting picked up...
os.getcwd()
# use creds to create a client to interact with the Google Drive API
scope = ['https://docs.google.com/spreadsheets/d/1PBB1eJ7zbcLyj7vsdrB8nEyZ9Ri0Nds8M2yFB0zEN1Q/edit#gid=0']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Sheet1").sheet1
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
当我尝试运行该脚本时,我收到此错误消息。
Traceback (most recent call last):
File "<ipython-input-34-e695bcd89439>", line 10, in <module>
client = gspread.authorize(creds)
File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\__init__.py", line 38, in authorize
client.login()
File "C:\Users\ryans\Anaconda3\lib\site-packages\gspread\client.py", line 51, in login
self.auth.refresh(http)
File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 545, in refresh
self._refresh(http)
File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 749, in _refresh
self._do_refresh_request(http)
File "C:\Users\ryans\Anaconda3\lib\site-packages\oauth2client\client.py", line 819, in _do_refresh_request
raise HttpAccessTokenRefreshError(error_msg, status=resp.status)
HttpAccessTokenRefreshError: invalid_scope: Invalid oauth scope or ID token audience provided.
我的“Google Drive API”和“Google Sheets API”都已启用。我按照下面链接中列出的 7 个步骤进行操作。
最后,我点击了 Google 表格页面上的“分享”。尽管如此,我还是遇到了这个奇怪的错误,如上所述。
注意:这是我的 Google 表格的图片。
【问题讨论】:
标签: python python-3.x google-sheets google-sheets-api gspread