1

Google Drive is not listing files in a folder when I give the folder id, nor when I give a mime type. It works for files in root folder with empty query. How do I list files in a folder?

Code:

public ArrayList<String> getDriveList(String folder, String folderId) throws IOException {
    //  Create a new authorized API client
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    Credential credential = Utils.getCredential();
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("SDM Mailer App")
        .build();
    ArrayList<File> result = new ArrayList<File>();
//      String query = "";
    String query = "'"+folderId+"' in parents";
//      String query = "mimeType = 'application/vnd.ms-excel'";
//      String query = "'"+folderId+"' in parents and mimeType = 'application/vnd.ms-excel'";
    log.info("Finding files with query: " + query);
    Drive.Files.List request = service.files().list().setQ(query);

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.err.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    log.info("Found # results: " + result.size());
    ArrayList<String> resultPlain = new ArrayList<String>();
    for (File f: result) {
        resultPlain.add(f.getTitle());
    }
    return resultPlain;

} // driveList

Log:

INFO: Finding files with query: '0B72jLeZWY3rsQllNMWhCQWhtLWc' in parents
Oct 04, 2013 8:50:42 PM com.example.server.SDMServiceImpl getDriveList
INFO: Found # results: 0

or with empty query, root folder:

INFO: Finding files with query: 
Oct 04, 2013 9:20:05 PM com.example.server.SDMServiceImpl getDriveList
INFO: Found # results: 2

Drive Files:

Imgur

Reference Documentation:

https://google-developers.appspot.com/drive/search-parameters

JAR:

google-api-services-drive-v2-rev102-1.16.0-rc.jar

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • 1
    Try running your query using the API explorer here https://developers.google.com/drive/v2/reference/files/list#try-it . That will tell you if it's a coding problem or a Drive setup problem. If it works in API Explorer, and not your app, you can look at the http request for each and see what is different. One possible explanation would be if your app had insufficient scope to read the files., eg. if it has drive.file but the files were created through the web. – pinoyyid Oct 05 '13 at 06:51
  • @pinoyyid Uh, the API explorer example doesn't work. It says 'Invalid callback url' after you authorize. The two files it does read were uploaded with the Getting Started example. All other files were drag&drop to the browser into Drive. These are the scopes I'm authenticating with: `https://www.googleapis.com/auth/drive.file`, `https://www.googleapis.com/auth/userinfo.email`, `https://www.googleapis.com/auth/userinfo.profile`. – Chloe Oct 06 '13 at 00:59
  • Cool it turns out it was the scopes! Changed to `drive.readonly`. Thanks. – Chloe Oct 06 '13 at 01:14

0 Answers0