0
votes

Good day all

I'm having a bit of a problem when it comes to downloading a file from Google Drive using a FileID. I've looked all over the place for answers, but I can't find one that works with my program.

I've been using two pieces of code: one from the Google Developers website here, and one from the Google API samples at GitHub. I have so far been unable to download a file from my Drive using a FileID.

My code is as follows:

public class DownloadTask extends SwingWorker<Void, Void> {

private String downloadURL;
private UpdateGUI gui;
private final String APPLICATION_NAME = "PROGRAM";
private final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".store/program");
private FileDataStoreFactory dataStoreFactory;
private HttpTransport httpTransport;
private final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private Drive drive;

public DownloadTask(String downloadURL, UpdateGUI gui) {
    this.downloadURL = downloadURL;
    this.gui = gui;
}

private Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(this.getClass().getResourceAsStream("/client_secrets.json")));
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton(DriveScopes.DRIVE)).setDataStoreFactory(dataStoreFactory)
            .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

/**
 * Executed in background thread
 */
@Override
protected Void doInBackground() throws Exception {
    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        // authorization
        Credential credential = authorize();
        // set up the global Drive instance
        drive = new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(
                APPLICATION_NAME).build();

        // run commands
        downloadFile(false, downloadURL);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Error downloading file: " + ex.getMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
        cancel(true);
        System.exit(1);
    }
    return null;
}

private void downloadFile(boolean useDirectDownload, String fileLocation)
        throws IOException {
    OutputStream out = new FileOutputStream(new java.io.File("DownloadedFile.jar"));

    GenericUrl url = drive.files().get(fileLocation).buildHttpRequestUrl();

    MediaHttpDownloader downloader
            = new MediaHttpDownloader(httpTransport, drive.getRequestFactory().getInitializer());
    downloader.setDirectDownloadEnabled(useDirectDownload);
    downloader.setProgressListener(gui);
    downloader.download(url, out);
}

@Override
protected void done() {
    //extra code
}
}

The downloadURL variable is the FileID, which I read from a database. The file I am downloading is a JAR file (Executable).

If there is any other information I would need to provide, please let me know.

1

1 Answers

0
votes

Try using the code from the Download Files document

Using Drive API client libraries.

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
        .executeMediaAndDownloadTo(outputStream);

Also try check SO post to check the exportLink (available in v2) but in v3 try using Files: export.

Note: Make sure the file ID is present in the Drive API. Incorrect fileID may result to error 404.

Hope this helps.