【问题标题】:Spreadsheet Not Found Error - Trying to Access Google Sheets未找到电子表格错误 - 尝试访问 Google 表格
【发布时间】:2020-03-10 13:15:26
【问题描述】:

我正在测试这段代码。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API

scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive.readonly']

creds = ServiceAccountCredentials.from_json_keyfile_name('C:\\my_path\\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("Test_Sheet").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

我收到此错误:

SpreadsheetNotFound

我按照下面链接中的说明进行操作。

https://medium.com/datadriveninvestor/use-google-sheets-as-your-database-using-python-77d40009860f

也许我错过了链接中的一步,但我不这么认为。自从那篇文章发表以来,GCP 可能发生了一些变化,大约一年多前。这很可能,因为云是一个非常动态的地方。解决此类问题的最佳方法是什么?我真的很想让这个东西发挥作用,主要是作为一种学习练习,但我也可以很快看到这在我的工作中非常有用。谢谢。

【问题讨论】:

  • 您能否尝试使用其他两种方法打开here 列出的电子表格?也许您的 API 无权访问相关文件。
  • 哦,这很好。我尝试了所有三种方法;没有工作。我收到此错误:APIError: {'code': 404, 'message': 'Requested entity was not found.', 'status': 'NOT_FOUND'}。我猜某些凭据或某种权限设置不正确。我会深入研究,看看发生了什么。我希望该错误更具描述性,这样您就可以隔离问题并修复它。
  • 在谷歌搜索您的新错误消息时,我在 Github 上发现了一个可能相关的未解决问题。你可以找到它here
  • 您是否尝试过Python quickstart guide 使用服务帐户凭据而不是通常的凭据?
  • 另外,您是否尝试过访问其他电子表格/表格以确保问题与授权/令牌步骤更相关?

标签: python python-3.x google-sheets google-sheets-api


【解决方案1】:

好吧,我终于想通了。在这里面对植物...

在名为“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2017-01-23
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多