我们如何从 Google Sheets 读取到 Python 中的 DF?

How can we read from Google Sheets into a DF in Python?

我正在运行宁这个代码(完全一样)。

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']

无论如何,我按照link中的说明进行了操作。

https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

我点击了 'My Projectt' > 'Service Account' > 'Enable'。我下载了JSON文件,命名为'client_secret.json',放在这个目录下:'C:\Users\ryans\client_secret.json'。最后,我打开 json 文件,得到 'client_email inside client_secret.json' 并将其放入 'Share' 并点击 'Save' 按钮。现在,当我 运行 上面的脚本时,我收到此错误消息:

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 Sheet 看起来像这样:

现在,我将代码更改为:

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' 都已启用。我遵循了下面 link 中列出的 7 个步骤。

https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

最后,我在 Google Sheet 页面上单击了 'Share'。如上所述,我仍然遇到这个奇怪的错误。

注意:这是我的 Google Sheet.

的图像

这个答案怎么样?请将此视为几个可能的答案之一。

问题与解决方案:

我看gspread的脚本的时候好像是open("Sheet1")是运行的时候,用的是DriveAPI的files.list方法。 Ref 在您的脚本中,只有 https://spreadsheets.google.com/feeds 用于作用域。我认为由此, insufficientPermissions 的错误发生了。为了避免这种情况,下面的修改怎么样?

修改后的脚本:

从:
scope = ['https://spreadsheets.google.com/feeds']
到:
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']
  • https://spreadsheets.google.com/feeds 适用于表格 API v3。 Sheets v3 API 已于 2020 年 9 月 30 日关闭。所以在这种情况下,我建议使用 https://www.googleapis.com/auth/spreadsheets 作为 Sheets API 的使用范围。此外,gspread 使用 Sheets API v4.

注:

  • 如果要使用修改文件元数据的方法,请使用https://www.googleapis.com/auth/drive的范围,而不是https://www.googleapis.com/auth/drive.readonly

参考文献:

(代表问题作者发布解决方案,将其从问题 post 中移走).

我终于明白这是怎么回事了。在名为 'statup_funding.json' 的文件中,您必须获取生成的电子邮件,然后单击电子表格上的 'Share' 按钮,并将该电子邮件地址粘贴到打开的 window 中。然后,一切都按预期进行。

这是我现在可以使用的代码的最终版本。

import gspread
#Service client credential from oauth2client
from oauth2client.service_account import ServiceAccountCredentials
# Print nicely
import pprint
#Create scope
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
#create some credential using that scope and content of startup_funding.json
creds = ServiceAccountCredentials.from_json_keyfile_name('startup_funding.json',scope)
#create gspread authorize using that credential
client = gspread.authorize(creds)
#Now will can access our google sheets we call client.open on StartupName
sheet = client.open('Test_Sheet').sheet1
pp = pprint.PrettyPrinter()
results = sheet.get_all_records()
results