0

I want to get list of files from google drive folder using google drive apis.

I have referred this stack overflow question

Above is not working for v3 apis. I think they have removed that support in version 3. I also referred this doc But it is not clear how it works.

I am using java sdk.

sdk
  • 178
  • 2
  • 19

2 Answers2

1

Take a look at version 3 (v3):

https://developers.google.com/drive/v3/web/quickstart/java

There is some example Java code (do search for "public class Quickstart").

Looking at the main method - you get a Drive, and can then page "through" files using:

FileList result = service.files().list()
         .setPageSize(10)
         .setFields("nextPageToken, files(id, name)")
         .execute();

Here is the main method:

public static void main(String[] args) throws IOException {
    // Build a new authorized API client service.
    Drive service = getDriveService();

    // Print the names and IDs for up to 10 files.
    FileList result = service.files().list()
         .setPageSize(10)
         .setFields("nextPageToken, files(id, name)")
         .execute();
    List<File> files = result.getFiles();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getName(), file.getId());
        }
    }
}
chocksaway
  • 870
  • 1
  • 10
  • 21
  • 1
    Thanks @chocksaway. It worked after adding setQ method. FileList files = service.files().list() .setFields("nextPageToken, files(id, name,parents)") .setQ("'folder_id' in parents").execute(); – sdk Jun 21 '17 at 07:02
  • Which package do I need to import to access getDriveService? Thank you. – d-b Jul 04 '18 at 08:44
0

This should work with udpdated URI:

GET https://www.googleapis.com/drive/v3/files

You could try the online demo with OAuth at https://developers.google.com/drive/v3/reference/files/list where you will find some more information on how to use the API and the OAuth.

H. Evers
  • 78
  • 7