Home > Mobile >  Google Drive API Wont Download Files
Google Drive API Wont Download Files

Time:11-25

I am trying to download the latest version of my software from google drive (I dont care about security, Im the only one running it, and im not sharing it), but it says the file is not found when its downloading.

Here is my code:

import os
import re
import sys
import functions
from functions import *
import json
from Google import Create_Service
import io
from googleapiclient.http import MediaIoBaseDownload

versionList = []
onlineVersionList = []
version = ""


#Google Api Stuff
CLIENT_SECRET_FILE = 'client_secret_GoogleCloud.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.install']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

#Searches for the highest version on google drive
page_token = None
while True:
    response = service.files().list(q="mimeType = 'application/vnd.google-apps.folder' and  name contains 'Version'"
                                      "and not name contains 'ClockOS'",
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        onlineVersionList.append(file.get('name'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break

for filename in os.listdir('Versions'):
    if filename.startswith('Version'):
        versionList.append(filename)
        print(f"Loaded {filename}")


def major_minor_micro(version):
    major, minor, micro = re.search('(\d )\.(\d )\.(\d )', version).groups()

    return int(major), int(minor), int(micro)

def major_minor(version):
    major, minor, micro = re.search('(\d )\.(\d )\.(\d )', version).groups()

    return int(major), int(minor)

def major(version):
    major, minor, micro = re.search('(\d )\.(\d )\.(\d )', version).groups()

    return int(major)


if versionType() == "stable":
    latest = str(max(versionList, key=major))
    onlineLatest = str(max(onlineVersionList, key=major))
elif versionType() == "standard":
    latest =  str(max(versionList, key=major_minor))
    onlineLatest = str(max(onlineVersionList, key=major_minor))
elif versionType() == "beta":
    latest = str(max(versionList, key=major_minor_micro))
    onlineLatest = str(max(onlineVersionList, key=major_minor_micro))
else:
    print("An error has occurred and a wrong version type was picked.")
    sys.exit()

if str(onlineLatest) > str(latest):
    #Gets the api of the highest version
    page_token = None
    while True:
        response = service.files().list(q=f"mimeType = 'application/vnd.google-apps.folder' and  name contains '{onlineLatest}'",
                                              fields='nextPageToken, files(id, name)',
                                              pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            print('Found file id: %s (%s)' % (file.get('name'), file.get('id')))
            onlineVersionID = file.get('name')
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    request = service.files().get_media(fileId=onlineVersionID)
    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)



print("Ran", latest)
setInfo("version", latest)

os.chdir(("Versions/" latest))
cwd = os.getcwd()
sys.path.append(cwd)

import main

And I get this error, after I log into google:

googleapiclient.errors.HttpError: 
<HttpError 404 when requesting (link to file)?alt=media returned "File not found: Version 0.1.1.". Details: "[
{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: Version 0.1.1.', 
'locationType': 'parameter', 'location': 'fileId'}
]">

It clearly finds it, as it returns the name I gave it, so why wont it download, anyone know how to fix? I already gave it scopes needed, and gave my account tester, and this is stored in the same google account as the one I log into.

CodePudding user response:

From It clearly finds it, as it returns the name I gave it, I thought that you might be able to retrieve the file. And, when I saw your script, it seems that the filename is used as the file ID with onlineVersionID = file.get('name') and request = service.files().get_media(fileId=onlineVersionID). So in this case, how about the following modification?

From:

onlineVersionID = file.get('name')

To:

onlineVersionID = file.get('id')
  • Related