Using gspread to read from a Google Drive Spreadsheet without logging in

旧巷老猫 提交于 2020-07-22 11:34:25

问题


I'm using the module gspread to (attempt) to parse a specific spreadsheet on my Google Drive. However, I eventually want to turn this project into a web application, so naturally I don't want to give away my login information. However, since I only need to read the spreadsheet, shouldn't there be a way to use gspread to read the contents of the file without having to log in? I've tried:

gc = gspread.Client(None)
sheet = gc.open_by_key(KEY)

But this fails with the error

(<class 'xml.etree.ElementTree.ParseError'>, ParseError(ExpatError('no element found: line 1, column 0',),), None)

How can I achieve this?


回答1:


To be honest I don't know what the error is coming from. Pasting more of the stack trace and some additional parts of your code might help.

Anyway, as you mentioned yourself you prefer to not use your login information (email/password).

GSpread also offers to log in by using gspread.Client(auth, http_session=None), where the auth parameter is OAuth2 credential object. Credential objects are those created by the oauth2client library.

I found this link very helpful to get the hang of it and getting it up working.

You won't log in anymore with your account, which is good, instead it will obtain OAuth 2.0 client credentials from the Google Developers Console. Then your client application requests an access token from the Google Authorization Server, extracts a token from the response, and sends the token to the Google API that you want to access.




回答2:


Have you made your spreadsheet public to the web? With that you can do what you want, I've posted code samples Here Loading a generic Google Spreadsheet in Pandas



来源:https://stackoverflow.com/questions/31362190/using-gspread-to-read-from-a-google-drive-spreadsheet-without-logging-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!