我们如何用Python将Google工作表读入DF?

我们如何用Python将Google工作表读入DF?,python,python-3.x,google-sheets,google-sheets-api,gspread,Python,Python 3.x,Google Sheets,Google Sheets Api,Gspread,我正在运行此代码(完全相同) 我不完全确定这句话是否正确: scope = ['https://spreadsheets.google.com/feeds'] 不管怎样,我遵循了这个链接中的说明 我单击了“我的项目”>“服务帐户”>“启用”。我下载了JSON文件,将其命名为“client\u secret.JSON”,并将其放在以下目录中:“C:\Users\ryans\client\u secret.JSON”。最后,我打开json文件,得到“client_secret.json中的cli

我正在运行此代码(完全相同)

我不完全确定这句话是否正确:

scope = ['https://spreadsheets.google.com/feeds']
不管怎样,我遵循了这个链接中的说明

我单击了“我的项目”>“服务帐户”>“启用”。我下载了JSON文件,将其命名为“client\u secret.JSON”,并将其放在以下目录中:“C:\Users\ryans\client\u secret.JSON”。最后,我打开json文件,得到“client_secret.json中的client_email”并将其放入“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.'}

我的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.
回溯(最近一次呼叫最后一次):
文件“”,第10行,在
client=gspread.authorize(creds)
文件“C:\Users\ryans\Anaconda3\lib\site packages\gspread\\uuuu init\uuuuu.py”,第38行,在“授权”中
client.login()
文件“C:\Users\ryans\Anaconda3\lib\site packages\gspread\client.py”,第51行,登录
self.auth.refresh(http)
文件“C:\Users\ryans\Anaconda3\lib\site packages\oauth2client\client.py”,第545行,刷新
自我刷新(http)
文件“C:\Users\ryans\Anaconda3\lib\site packages\oauth2client\client.py”,第749行,在\u刷新中
self.\u do\u refresh\u请求(http)
文件“C:\Users\ryans\Anaconda3\lib\site packages\oauth2client\client.py”,第819行,在\u do\u refresh\u请求中
raise HttpAccessTokenRefreshError(错误消息,状态=响应状态)
HttpAccessTokenRefreshError:无效的\u作用域:提供的oauth作用域或ID令牌访问群体无效。
我的“Google Drive API”和“Google Sheets API”都已启用。我遵循了下面链接中列出的7个步骤

最后,我在谷歌表单页面上点击了“共享”。尽管如此,我还是得到了这个奇怪的错误,如上所述

注意:这是我的谷歌工作表的图片


这个答案怎么样?请把这看作是几个可能的答案之一

问题和解决办法: 当我看到gspread的脚本时,似乎在运行
open(“Sheet1”)
时,使用了驱动API的files.list方法。在脚本中,只有
https://spreadsheets.google.com/feeds
用于作用域。我认为这样会出现
权限不足
的错误。为了避免这种情况,下面的修改如何

修改脚本: 发件人: 致:
  • https://spreadsheets.google.com/feeds
    适用于工作表API v3。API将于2020年9月30日关闭。因此,在这种情况下,我建议使用
    https://www.googleapis.com/auth/spreadsheets
    作为使用API的范围。此外,gspread使用API v4
注:
  • 如果要使用修改文件元数据的方法,请使用
    https://www.googleapis.com/auth/drive
    而不是
    https://www.googleapis.com/auth/drive.readonly
参考资料:
(代表问题作者发布解决方案,将其从问题帖子中移除)

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

这是我现在正在工作的代码的最终版本

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

嘿,塔奈克。我刚刚更新了我原来的帖子,从“更新:”开始。关于我如何使这项工作正常进行,你还有其他想法吗?谢谢。@ASH谢谢你的回复。给您带来不便,我深表歉意。当我看到您更新的问题时,范围使用
scope=['https://docs.google.com/spreadsheets/d/1PBB1eJ7zbcLyj7vsdrB8nEyZ9Ri0Nds8M2yFB0zEN1Q/edit#gid=0']
。此值不是正确的范围。那么,如何修改为
scope=['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly“]
?我刚试过你的建议。现在,我得到了这个:回溯(最近一次调用):文件“”,第16行,在sheet=client.open('Sheet1')。Sheet1文件“C:\Users\ryans\Anaconda3\lib\site packages\gspread\client.py”,在open raise SpreadsheetNotFound中的第131行SpreadsheetNotFound@ASH谢谢你的回复。给您带来的不便,我深表歉意。从您的回复中,我可以确认范围和授权问题已经解决。关于新的错误消息,这意味着找不到
Sheet1
的电子表格名称。因此,我认为您可能误解了
sheet=client.open(“Sheet1”).Sheet1的
Sheet1
。但是,不幸的是,我不确定电子表格标题,也就是您要使用的电子表格文件名。因此,请再次确认电子表格标题并设置
sheet=client的
#####
。打开(“####”)。sheet1
。您的解决方案确实帮助我朝着正确的方向前进!我刚刚把你原来的帖子投了上去!!
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.
scope = ['https://spreadsheets.google.com/feeds']
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']
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