Shashishekhar Hasabnis
Shashishekhar Hasabnis

Reputation: 1756

How to access google sheets without authentication?

I have watched the following video where a demo is given about accessing google sheets without authentication:-

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

As per the video I am unable to access my google sheet in pandas with the following code:-

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()

The exception I get is:-

HTTP Error 400: Bad Request

This same code works for the following google sheet which is public:- https://docs.google.com/spreadsheets/d/1x9p3nqTf9fBx8I4lb3WMC_K0BLaIU8Z-7kj7MVani3U/edit?usp=sharing where the sheet_id = "1x9p3nqTf9fBx8I4lb3WMC_K0BLaIU8Z-7kj7MVani3U"

Do I need to add some headers/parameters for this to work?

Upvotes: 1

Views: 2510

Answers (1)

Tanaike
Tanaike

Reputation: 201473

How about modifying the endpoint as follows? This endpoint can be seen at exportLinks of the method of "Files: get" in Drive API. Ref

From:

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

To:

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

Note:

  • When I tested your sample Spreadsheet, the data could be retrieved. In this case, the Spreadsheet is required to be publicly shared. Please be careful this.

Reference:

Upvotes: 1

Related Questions