43

I am working on an Android project and I am trying to make use of the Google Drive API and I've got most of it working but I am having an issue in how I perform a download.

I can't find anywhere how I get the file ID so that I can perform the download. In order to test I've retrieved all files in my drive account and added them to a list array and then got the ID for the first file in the list.

I then copied the file ID in to the command to download the file and this worked successfully but I have no idea how to get the file id of the specific file I want to download.

Below is the code I am using to download the file.

private void downloadFile()
{
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try 
            {
                com.google.api.services.drive.model.File file = service.files().get("0B-Iak7O9SfIpYk9zTjZvY2xreVU").execute();
                //FileList file = service.files().list().execute();
                //List<com.google.api.services.drive.model.File> fileList = file.getItems();
                //com.google.api.services.drive.model.File fileItem = fileList.get(0);
                //Log.d("FileID" , fileItem.getId());
                //Log.d("Count", "Retreived file list");
                if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
                {
                    HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
                    InputStream inputStream = resp.getContent();
                    writeToFile(inputStream);
                }
            } 
            catch (IOException e)
            {
                Log.e("WriteToFile", e.toString());
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

0B-Iak7O9SfIpYk9zTjZvY2xreVU is the file ID of the file that I download which I retrieved when I did a list and selected the first file, but how can I say I want to download, File_1 and get its ID to then pass this to the service.get().execute function.

Basically my end goal is within my app, I upload an XML file to Google Drive, and then later on Download the file. It will only be one file and will always have the same name. Am I going about it the right away or is there a better way to achieve what I am trying to do?

halfer
  • 19,824
  • 17
  • 99
  • 186
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • can i get the complete code? or atleast the dependencies that you are using for google drive api – NullByte08 Jun 14 '20 at 18:34
  • In my opinion the easiest and fastest way to get a Google Drive file ID is from Google Drive on the web. Right-click the file name and select Get shareable link. The last part of the link is the file ID. Then you can cancel the sharing. – Charlie Parker Jan 22 '21 at 18:00
  • [I've answered a very similar question using Python](https://stackoverflow.com/a/62410240/5675325). Hope that helps! – Tiago Martins Peres Nov 08 '22 at 13:35

7 Answers7

108

In my opinion the easiest and fastest way to get a Google Drive file ID is from Google Drive on the web. Right-click the file name and select Get shareable link. The last part of the link is the file ID. Then you can cancel the sharing.

RomRoc
  • 1,465
  • 2
  • 12
  • 12
12

This script logs all the file names and ids in the drive:

// Log the name and id of every file in the user's Drive
function listFiles() {
  var files = DriveApp.getFiles();
  while ( files.hasNext() ) {
    var file = files.next();
    Logger.log( file.getName() + ' ' + file.getId() );
  }
}  

Also, the "Files: list" page has a form at the end that lists the metadata of all the files in the drive, that can be used in case you need but a few ids.

Juan Lanus
  • 2,293
  • 23
  • 18
8

Well the first option I could think of is that you could send a list request with search parameters for your file, like title="File_1.xml" and fileExtension="xml". It will either return an empty list of files (there isn't one matching the serach criteria), or return a list with at least one file. If it's only one - it's easy. But if there are more - you'll have to select one of them based on some other fields. Remember that in gdrive you could have more than 1 file with the same name. So the more search parameters you provide, the better.

stan0
  • 11,549
  • 6
  • 42
  • 59
  • Thanks for your help, that seems a lot of work to tell android to get a list of all files and then iterate over all of them to find the file I want so I can get the ID. Was expecting to just provide a file name and it retrieves the ID. Thanks, though I will give it a try – Boardy Feb 25 '13 at 13:33
  • You are not actually telling android to do it, but the google's servers. This way their servers are doing your job (filtering the right file(s) from the (potentially long) list of files). Plus there's less data transferred over the network. – stan0 Feb 25 '13 at 13:42
  • Yea I see your point, just seemed quite slow and long winded, when I did the list to find what the file id was for my test, mentioned in the question it took a few seconds to complete and I don't that an overly large number of files in my Drive account – Boardy Feb 25 '13 at 18:22
  • You could also try keeping the id for further references. I think I haven't read anything about this, but maybe the id is constant. I'm not sure though. It's just an idea. – stan0 Feb 26 '13 at 10:54
  • The id is constant, and persistent even if the name of the file changes. – Joe Sep 26 '15 at 10:10
6

Click with the right mouse button on the file in your Google Drive. Choose the option to get a link which can be shared from the menu. You will see the file id now. Don't forget to undo the share.

Marijn As
  • 69
  • 1
  • 1
5

One way is to associating unique properties with your file while creation.

properties = "{ \
    key='somekey' and \
    value='somevalue'
}"

then create a query.

query = "title = " + "\'" + title + "\'" + \
        AND + "mimeType = " + "\'" + mimeType + "\'" + \
        AND + "trashed = false" + \
        AND + "properties has " + properties

All the file properties(title, etc) already known to you can go here + properties.

Amber Kulkarni
  • 424
  • 8
  • 17
2

Stan0 intial idea is not a good idea. There can be multiple files with the same name. Very error prone implementation. Stan0's second idea is the correct way.

When you first upload the file to google drive store its id (in SharedPreferences is probably easiest) for later use

ie.

file= mDrive.files().insert(body).execute();      //initial insert of file to google drive

whereverYouWantToStoreIt= file.getId(); //now you have the guaranteed unique id 
                                        //of the file just inserted. Store it and use it 
                                        //whenever you need to fetch this file
Ian
  • 147
  • 2
  • 9
2

If you know the the name of the file and if you always want to download that specific file, then you can easily get the ID and other attributes for your desired file from: https://developers.google.com/drive/v2/reference/files/list (towards the bottom you will find a way to run queries). In the q field enter title = 'your_file_name' and run it. You should see some result show up right below and within it should be an "id" field. That is the id you are looking for.

You can also play around with additional parameters from: https://developers.google.com/drive/search-parameters