codebot
codebot

Reputation: 2646

Google Drive Java API file parents are not fetching correctly

I have a Java program which I use to fetch folders and files from Google Drive. I'm building the path for the file by traversing the parents of each file. When I create a folder in one of my personal G Drives, and share with the SA email, it gives correct parents and path.

From my organization they have shared a folder with me and I've added the SA email in shared list as an Editor. but some of the parents are not fetching.

Code to fetch folders

Drive.Files.List foldersRequest = service.files().list()
            .setPageSize(1000).setSupportsAllDrives(true).setIncludeItemsFromAllDrives(true)
            .setQ("trashed=false and mimeType='application/vnd.google-apps.folder'")
            .setFields("nextPageToken, files(id, name, mimeType, modifiedTime,webContentLink,parents," +
                    "createdTime)").setPageToken(nextPageTokenFolders);

Code to fetch files

Drive.Files.List filesRequest = service.files().list()
            .setPageSize(1000).setSupportsAllDrives(true).setIncludeItemsFromAllDrives(true)
            .setQ("trashed=false and mimeType='application/pdf'")
            .setFields("nextPageToken, files(id, name, mimeType, modifiedTime,webContentLink,parents,createdTime)")
            .setPageToken(nextPageTokenFiles);

Code to get full path

private static String getFullPath(List<File> files, String parentId) {
    String path = "";
    String currentFilePath = "";
    for (File file : files) {
        if (file.getParents() != null) {
            if (file.getId().equals(parentId)) {
                path = path.concat(currentFilePath + "/" + file.getName());
                parentId = file.getParents().get(0);
            }
        }
    }

    String[] completePath = path.split("/");
    String finalPath = "";
    for (int i = completePath.length - 1; i >= 0; i--) {
        finalPath = finalPath.concat(completePath[i] + "/");
    }

    return finalPath.substring(0, finalPath.length() - 1);
}

With this code I'm able to get the corrects path for folders in personal accounts. But it misses some parent folders when I'm running the same code with organization G Drive folder. The service account access has set as Editor.

Upvotes: 0

Views: 95

Answers (0)

Related Questions