Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google drive API download files - python - no files downloaded

Tags:

I have tried a number of ways to download files from google drive via oauth and the API, however I am not able to get the files downloaded. I believe I have properly authenticated. After running my code, it looks like there was success with downloading the file (no errors), but no files were downloaded.

This is the code I have tried so far:

def download_file(file_id, mimeType):     if "google-apps" in mimeType:         return     request = drive_service.files().get(fileId=file_id)     fh = io.BytesIO()     downloader = MediaIoBaseDownload(fh, request)     done = False     while done is False:         status, done = downloader.next_chunk()         print "Download %d%%." % int(status.progress() * 100) 

However, this results in "Download 100%." being printed to the console, but no file downloaded.

I have also tried:

def download2(download_url):     resp, content = drive_service._http.request(download_url)     if resp.status == 200:         print 'Status: %s' % resp         return content     else:         print 'An error occurred: %s' % resp         return None 

This also does not produce a downloaded file, but it does give me a 200 message.

Both of these seem like they are properly making contact with the API. Is there an additional step I have to do to actually get the files on my computer?

Edit:

this was the remainder of my code:

import json import webbrowser  import httplib2 import io from apiclient.http import MediaIoBaseDownload  from apiclient import discovery from oauth2client import client  if __name__ == '__main__':   flow = client.flow_from_clientsecrets(     'client_secrets.json',     scope='https://www.googleapis.com/auth/drive.readonly',     redirect_uri='urn:ietf:wg:oauth:2.0:oob')    auth_uri = flow.step1_get_authorize_url()   webbrowser.open(auth_uri)    auth_code = raw_input('Enter the auth code: ')    credentials = flow.step2_exchange(auth_code)   http_auth = credentials.authorize(httplib2.Http())    drive_service = discovery.build('drive', 'v3', http_auth) #also tried v2   files = drive_service.files().list().execute()   for f in files['files']:     #call one of the two download methods with the proper arguments 
like image 828
user1253952 Avatar asked Mar 23 '16 08:03

user1253952


People also ask

Why is Google Drive not showing downloads?

Quite frequently the Google Drive not downloading can be attributed to your browser's settings, specifically to it saving something incorrectly in its cache files and cookies. So clearing them both could help. When it comes to getting rid of cookies and cache, every browser is different.

How do I download from Google Drive API?

To download a file stored on Google Drive, use the files. get method with the ID of the file to download and the alt=media URL parameter. The alt=media URL parameter tells the server that a download of content is being requested.


1 Answers

Changing from BytesIO to FileIO allowed the file to actually be downloaded. This was the line I modified my code to:

fh = io.FileIO(filename, 'wb') 

Here is the complete code that allowed me to download the file:

def download_file(file_id, mimeType, filename):     if "google-apps" in mimeType:         # skip google files         return     request = drive_service.files().get_media(fileId=file_id)     fh = io.FileIO(filename, 'wb')     downloader = MediaIoBaseDownload(fh, request)     done = False     while done is False:         status, done = downloader.next_chunk()         print "Download %d%%." % int(status.progress() * 100)   if __name__ == '__main__':     flow = client.flow_from_clientsecrets(       'client_secrets.json',       scope='https://www.googleapis.com/auth/drive.readonly',       redirect_uri='urn:ietf:wg:oauth:2.0:oob')      auth_uri = flow.step1_get_authorize_url()     webbrowser.open(auth_uri)      print auth_uri      auth_code = raw_input('Enter the auth code: ')      credentials = flow.step2_exchange(auth_code)     http_auth = credentials.authorize(httplib2.Http())      drive_service = discovery.build('drive', 'v3', http_auth)     files = drive_service.files().list().execute()     for f in files['files']:         print f['name']         download_file(f['id'], f['mimeType'], f['name']) 
like image 108
user1253952 Avatar answered Sep 27 '22 21:09

user1253952