Google drive API download files - python - no files downloaded

Viewed 16191

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
2 Answers
Related