Gspread - 无法检索电子表格

Gspread - Can Not Retrieve Spreadsheets

我正尝试在 Python2.7 中使用 Gspread 来检索电子表格。我似乎能够登录,但每当我发出命令

gc.openall()

它只是 returns 一个空列表。我已授予服务帐户管理员访问所有内容的权限,并且在我的 google 控制台中启用了工作表 api。谁能指出我做错了什么?

下面是我的 name-hash.json 文件,由 Google 返回并用于登录:

{
  "type": "service_account",
  "project_id": "name-id",
  "private_key_id": "PRIVATE KEY ID",
  "private_key": "-----BEGIN PRIVATE KEY-----
  ...
  \n-----END PRIVATE KEY-----\n",
  "client_email": "test@mname-id.iam.gserviceaccount.com",
  "client_id": "CLIENTID",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-279%40name-id.iam.gserviceaccount.com"
}

我的代码如下:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

def login(json_key):
     scope = ['https://spreadsheets.google.com/feeds']
     credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key, scope)
     gc = gspread.authorize(credentials)
     return gc

gc = login('/Users/username/Downloads/name-hash.json')
files = gc.openall()
print(files)
    []

我能想到的主要有 2 个问题。首先 gspread 只能访问位于 google 驱动器根目录的 google sheets。为了避免这种情况,我强烈建议移动到 v4 google api python 客户端而不是 gspread。

如果您在根目录中确实有 google sheets,我猜您缺少与地址共享传播 sheets 本身:test@mname-id.iam.gserviceaccount.com

为了验证这一点,我建议用 open_by_key 替换 openall 并提供一个特定的 google sheet id 以帮助调试。