How to get folder ID using Google Drive API in Java

I need help creating a Java method that can find and return the ID of a specific folder using Google Drive API. I’ve been struggling with this for a while and the existing documentation doesn’t provide clear examples for my use case.

Here’s what I have so far but it’s not working correctly:

public void searchDirectoryById(String directoryName) throws MalformedURLException, IOException, ServiceException {
    
    URL apiEndpoint = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
    
    DocumentQuery searchQuery = new DocumentQuery(apiEndpoint);
    searchQuery.setTitleQuery(directoryName);
    searchQuery.setTitleExact(true);
    DocumentListFeed resultFeed = null;
    
    try {
        resultFeed = service.getFeed(searchQuery, DocumentListFeed.class);
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (ServiceException ex) {
        ex.printStackTrace();
    }
    
    for (DocumentListEntry item : resultFeed.getEntries()) {
        System.out.println(item.getDocId());
    }
}

The method runs without errors but never displays the folder ID I’m looking for. What am I doing wrong here?

Your problem is you’re using the old Google Documents List API instead of Drive API v3. I hit this same issue two years ago on a file management project.

Here’s what works with the current Drive API. Make sure you’ve got the right dependency and auth setup, then use this:

public String findFolderIdByName(String folderName) throws IOException {
    String pageToken = null;
    do {
        FileList result = driveService.files().list()
            .setQ("name='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and trashed=false")
            .setSpaces("drive")
            .setPageToken(pageToken)
            .execute();
        
        for (File file : result.getFiles()) {
            return file.getId(); // Returns first match
        }
        pageToken = result.getNextPageToken();
    } while (pageToken != null);
    
    return null; // Folder not found
}

The important bits: proper query syntax with mimeType filter and trashed=false to skip deleted folders. This’ll fix your issue.

That code’s using the old Google Documents List API - it got deprecated years ago. No wonder it’s broken.

For Google Drive API, you need the newer v3 API and query files with the right MIME type for folders:

public String getFolderId(String folderName) {
    String query = "name='" + folderName + "' and mimeType='application/vnd.google-apps.folder'";
    FileList result = driveService.files().list().setQ(query).execute();
    
    if (result.getFiles().isEmpty()) {
        return null;
    }
    
    return result.getFiles().get(0).getId();
}

Honestly though, Google API setup and auth is a massive pain. I’ve fought with it on tons of projects.

Now I just automate this stuff with Latenode. You can build a workflow that connects to Google Drive, searches folders by name, and returns the ID - no Java needed. Plus you can chain it with other operations like creating files or moving folders.

The visual interface makes error handling and testing way easier. Much less headache than debugging API calls.

You’re mixing up API versions. That DocumentQuery approach is dead - it was part of the old Documents List API.

Honestly, after fighting Google’s API auth on tons of projects, I found something way better. Skip the Java SDK headaches, OAuth nonsense, and version hell - just automate this with Latenode.

Build a workflow that searches Drive folders by name and grabs the ID in seconds. No code, no dependencies, no auth drama. Connect your Drive account and drag-and-drop the logic.

You can get fancy with it too - search specific parent folders, handle multiple matches, trigger other stuff when folders pop up. I use this for all my file automation now.

Saves me hours vs debugging Java API calls and keeps everything clean without touching code.

hey, looks like you’re stuck on old stuff! DocumentQuery is a no-go with Drive API v3. you gotta use files().list() and get your query right. they removed all that GData junk ages ago, so time to update your code.

This happens because you’re using deprecated Google Documents List API endpoints. Hit the same issue migrating legacy code last year.

DocumentQuery won’t work anymore - Google killed those endpoints. Switch to Drive API v3 and set up proper auth through Google Cloud Console.

Something other answers missed: handle multiple folders with identical names since Drive allows duplicates. Found this out the hard way when production code started grabbing wrong folder IDs.

public String getFolderIdInParent(String folderName, String parentId) throws IOException {
    String query = "name='" + folderName + "' and mimeType='application/vnd.google-apps.folder' and '" + parentId + "' in parents and trashed=false";
    
    FileList result = driveService.files().list()
        .setQ(query)
        .setFields("files(id, name)")
        .execute();
    
    return result.getFiles().isEmpty() ? null : result.getFiles().get(0).getId();
}

Specifying parent folder ID makes searches faster and more reliable. Don’t forget to enable Drive API in your Google Cloud project and configure OAuth2 credentials.