The Setup: Creating new apps using Google APIs
317 9 56037
For developers new to Google APIs, this episode shows you how to setup a new app ("project") in the Google Developers Console and how to obtain the credentials necessary to use our APIs with.
By anonymous 2017-09-20
(Feb 2017) The code in the OP and the only other answer are both now out-of-date as ClientLogin authentication was deprecated back in 2012(!), and GData APIs are the previous generation of Google APIs. While not all GData APIs have been deprecated, all newer Google APIs do not use the Google Data protocol.
To read plain text from a Google Doc, considered file-level access, you would use the Google Drive API instead. Examples of using the Drive API:
- Exporting a Google Sheet as CSV (blog post)
- "Poor man's plain text to PDF" converter (blog post) (*)
(*) - TL;DR: upload plain text file to Drive, import/convert to Google Docs format, then export that Doc as PDF. Post above uses Drive API v2; this follow-up post describes migrating it to Drive API v3, and here's a developer video combining both "poor man's converter" posts.
The solution to the OP is to perform similar operations as what you see in both posts above but ensure you're using the
text/plain export MIMEtype. For other import/export formats to/from Drive, see this related question SO answer as well as the downloading files from Drive docs page. Here's some pseudocode that searches for Google Docs documents called "Hello World" in my Drive folder and displays the contents of the first matching file found on-screen (assuming DRIVE is your API service endpoint):
from __future__ import print_function NAME = 'Hello World' MIME = 'text/plain' # using Drive API v3; if using v2, change 'pageSize' to 'maxResults', # 'name=' to 'title=', and ".get('files')" to ".get('items')" res = DRIVE.files().list(q="name='%s'" % NAME, pageSize=1).execute().get('files') if res: fileID = res['id'] # 1st matching "Hello World" name res = DRIVE.files().export(fileId=fileID, mimeType=MIME).execute() if res: print(res.decode('utf-8')) # decode bytes for Py3; NOP for Py2
If you need more than this, see these videos on how to setup using Google APIs, OAuth2 authorization, and creating a Drive service endpoint to list your Drive files, plus a corresponding blog post for all three.
By anonymous 2017-09-20
requests library is the gold standard for HTTP requests from Python, this style of download is (while not deprecated yet) not likely to last, specifically referring to the use of links, managing cookies & redirects, etc. One of the reasons for not preferring links is that it's less secure and generally such access should require authorization. Instead, the currently accepted way of exporting Google Sheets as CSV is by using the Google Drive API.
So why the Drive API? Isn't this supposed to be something for the Sheets API instead? Well, the Sheets API is for spreadsheet-oriented functionality, i.e., data formatting, column resize, creating charts, cell validation, etc., while the Drive API is for file-oriented functionality, i.e., import/export, copy, rename, etc.
Below is a complete cmd-line solution. (If you don't do Python, you can use it as pseudocode and pick any language supported by the Google APIs Client Libraries.) For the code snippet, assume the most current Sheet named
inventory (older files with that name are ignored) and
DRIVE is the API service endpoint:
FILENAME = 'inventory' SRC_MIMETYPE = 'application/vnd.google-apps.spreadsheet' DST_MIMETYPE = 'text/csv' # query for latest file named FILENAME files = DRIVE.files().list( q='name="%s" and mimeType="%s"' % (FILENAME, SRC_MIMETYPE), orderBy='modifiedTime desc,name').execute().get('files', ) # if found, export Sheets file as CSV if files: fn = '%s.csv' % os.path.splitext(files['name'].replace(' ', '_')) print('Exporting "%s" as "%s"... ' % (files['name'], fn), end='') data = DRIVE.files().export(fileId=files['id'], mimeType=DST_MIMETYPE).execute() # if non-empty file if data: with open(fn, 'wb') as f: f.write(data) print('DONE')
If your Sheet is large, you may have to export it in chunks -- see this page on how to do that. If you're generally new to Google APIs, I have a (somewhat dated but) user-friendly intro video for you. (There are 2 videos after that maybe useful too.)
Popular Videos 191
Submit Your Video