如何在未经身份验证的情况下访问Google表格?

时间:2020-11-09 04:54:57

标签: python google-sheets

我观看了以下视频,其中演示了有关未经身份验证访问Google表格的演示:-

https://youtu.be/t6WSY9D_ORQ?list=LL

根据视频,我无法使用以下代码访问熊猫的Google表格:-

import pandas as pd
sheet_id = "my_sheet_id"
r = "https://docs.google.com/spreadsheets/d/{}/export?format=csv".format(sheet_id)
df = pd.read_csv(r)
df.head()

我得到的例外是:-

HTTP Error 400: Bad Request

此代码适用于以下公开的Google表格:- https://docs.google.com/spreadsheets/d/1x9p3nqTf9fBx8I4lb3WMC_K0BLaIU8Z-7kj7MVani3U/edit?usp=sharing sheet_id = "1x9p3nqTf9fBx8I4lb3WMC_K0BLaIU8Z-7kj7MVani3U"

我是否需要添加一些标题/参数才能使其正常工作?

1 个答案:

答案 0 :(得分:1)

如何如下修改端点?可以在Drive API中“文件:获取”方法的exportLinks处看到此端点。 Ref

发件人:

r = "https://docs.google.com/spreadsheets/d/{}/export?format=csv".format(sheet_id)

收件人:

r = "https://docs.google.com/spreadsheets/export?id={}&exportFormat=csv".format(sheet_id)

注意:

  • 当我测试示例电子表格时,可以检索数据。在这种情况下,电子表格必须公开共享。请注意这一点。

参考: