如何从谷歌驱动器api v3获取文件列表?

如何从谷歌驱动器api v3获取文件列表?

问题描述:

我想从谷歌驱动器文件夹中使用谷歌驱动器API获取文件列表。如何从谷歌驱动器api v3获取文件列表?

我已经提到这个 stack overflow question

上面没有工作了V3的API。我认为他们已经删除了版本3中的支持。 我也提到了这个doc 但它不清楚它是如何工作的。

我正在使用java sdk。

看看第3版(V3):

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

有一些例如Java代码(请搜索 “公共类快速入门”)。

纵观主要方法 - 你得到一个驱动器,然后页 “通过” 文件中使用:

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

这里是主要的方法:

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()); 
     } 
    } 
} 
+1

感谢@ chocksaway。它在添加setQ方法后工作。 FileList files = service.files()。list() .setFields(“nextPageToken,files(id,name,parents)”) .setQ(“'folder_id'in parents”)。 – sdk

这应该与udpdated URI工作:

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

您可以尝试使用OAuth在https://developers.google.com/drive/v3/reference/files/list在线演示在那里你会找到如何使用API​​和OAuth的一些更多的信息。