I tried several ways to download files from Google Drive via oauth and the API, however I cannot upload files. I believe that I am authenticated correctly. After running my code, it looks like there was a successful file download (no errors), but the files were not loaded.
This is the code I've 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 leads to "Download 100%." prints to the console, but the file does not load.
I 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 create the downloaded file, but it gives me a 200 message.
Both of them seem to come into contact with the API correctly. Is there another step I must take to actually get the files on my computer?
Edit:
this is the rest 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)
source share