简体   繁体   中英

Download file from Google Drive - method is missing? (java)

I try to use the Java API of Google Drive to download all files in a certain folder. Since I am beginner with this API I googled and found several questions and answers about this here on SO, ee, Download files from google drive using java API

All these examples use the following construction:

if (file.getDownloadUrl() !=null && file.getDownloadUrl().length() > 0) {

However, when I use this (in Eclipse) (it is of course simplified)

com.google.api.services.drive.model.File myFile = new com.google.api.services.drive.model.File();
myFile.getDownloadUrl();

I get an error message The method getDownloadUrl() is undefined for the type File . Furthermore, googling for this method name does not reveal any meaningful results. Am I missing something very fundamental here?

Firstly, com.google.api.services.drive.model.File myFile = new com.google.api.services.drive.model.File(); myFile.getDownloadUrl(); com.google.api.services.drive.model.File myFile = new com.google.api.services.drive.model.File(); myFile.getDownloadUrl(); makes no sense. You are trying to assign a String (the return value of getDownloadUrl()) to a File object.

However, the compiler hasn't got round to complaining about this yet because it can't find the downloadUrl() method signature. My guess is that you are using the wrong jar file for the API. In this case I think you're trying to use the v2 API but have probably downloaded the v3 jar. It is better to follow the Google documentation rather than copy/paste code from StackOverflow.

The method file.getDownloadUrl() has been removed in Drive API v3.
Try to use the method executeMediaAndDownloadTo() instead of using getDownloadUrl() in order to download google drive files. See the following sample code .

The method executeMediaAndDownloadTo() in the Drive.File.Get class .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM