我尝试使用以下代码访问我的 Google 云端硬盘中的 Google 表格文件:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
wks = gc.open('my_test').sheet1
print(wks.get_all_records())
然后我创建了一个名为的电子表格my_test在我的云端硬盘中并通过电子邮件共享凭证.json。然后它就起作用了。然而,范围太广了,我想使用
scope = ['https://www.googleapis.com/auth/drive.file']
反而。但如果我改变范围,它就找不到工作表:
File "/home/my_username/.local/lib/python3.6/site-packages/gspread/client.py", line 130, in open
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
这是为什么?我怎样才能改变它?我在谷歌上搜索了很多,对于大多数人来说,共享或重新共享电子表格是有效的。就我而言,问题仍然存在。
我也尝试过这个使用drive.file范围执行查询时出现错误500 https://stackoverflow.com/questions/13693617/error-500-when-performing-a-query-with-drive-file-scope为云端硬盘元数据添加范围的解决方案,但这会导致此权限错误,这可以被视为进度(因为它找到了文件):
gspread.exceptions.APIError: {
"error": {
"code": 403,
"message": "The caller does not have permission",
"status": "PERMISSION_DENIED"
}
}
但是,即使我将电子表格访问权限设置为“公共”,此错误仍然存在。
任何提示都非常感谢!
哦,我有一个更普遍的问题:服务帐户是否可以访问我的整个驱动器?或者仅适用于与服务帐户共享的所有内容?因为我只与它共享了一个电子表格,所以我对这里的权限到底如何工作有点困惑。