问题
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