How To Download A File With Python-google-api
How would I download a file using the GoogleAPI? Here is what I have so far: CLIENT_ID = '255556' CLIENT_SECRET = 'y8sR1' DOCUMENT_ID = 'a123' service=build('drive', 'v2') # How t
Solution 1:
There are different ways to download a file using Google Drive API. It depends on whether you are downloading a normal file or a google document (that needs to be exporteed in a specific format).
for regular files stored in drive, you can either use:
alt=media and it's the preferred option, as in:
GET https://www.googleapis.com/drive/v2/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs
the other method is to use DownloadUrl, as in:
from apiclient import errors
# ...defdownload_file(service, drive_file):
"""Download a file's content.
Args:
service: Drive API service instance.
drive_file: Drive File instance.
Returns:
File's content if successful, None otherwise.
"""
download_url = drive_file.get('downloadUrl')
if download_url:
resp, content = service._http.request(download_url)
if resp.status == 200:
print'Status: %s' % resp
return content
else:
print'An error occurred: %s' % resp
returnNoneelse:
# The file doesn't have any content stored on Drive.returnNone
For google documents, instead of using downloadUrl, you need to use exportLinks and specify the mime type, for example:
download_url = file['exportLinks']['application/pdf']
The rest of the documentation can be found here: https://developers.google.com/drive/web/manage-downloads
Post a Comment for "How To Download A File With Python-google-api"