Google drive API download files - python - no files downloaded

user1253952 picture user1253952 · Mar 23, 2016 · Viewed 12.6k times · Source

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

Answer

user1253952 picture user1253952 · Mar 26, 2016

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'])