Home > Mobile >  Listing files in Google Drive Folder with Google API v3 JAVA
Listing files in Google Drive Folder with Google API v3 JAVA

Time:01-05

I am trying to list all files found in google drive folder, the query seems to work but when I try to get the files list using .getFiles() the list is empty.

    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String CREDENTIALS_FILE_PATH = "/serv_acc.json";
    private static final String SERVICE_ACCOUNT = "xxxxxxxxx.iam.gserviceaccount.com";

    private static final String APPLICATION_NAME = "ZZZ-bco-eXXX_oo";
    private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);
    private static final String ROOT_FOLDER = "XXXXXXXXXXXXXX";


    private static final String projection = "nextPageToken, files(id, name)";
    
    private static Drive driveService;
 

public static void buildApiService() throws IOException, GeneralSecurityException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        InputStream resourceAsStream = GoogleDriveApplication.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
                ServiceAccountCredentials.fromStream(resourceAsStream).createScoped(SCOPES).createDelegated(SERVICE_ACCOUNT));
        driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY,                       requestInitializer).setApplicationName(APPLICATION_NAME).build();
    }

public static List<File> getChildren(String id) throws IOException {

        String query = "'"   id   "'"   " in parents";

        FileList directChildren = driveService.files().list()
                .setQ(query).setFields(projection).execute();

        System.out.println(directChildren.isEmpty()); // False
        System.out.println(directChildren.size()); // 3
        System.out.println(directChildren.getFiles().isEmpty()); // True
        ...
    }

public static void main(String[] args) throws GeneralSecurityException, IOException {

        buildApiService();
       ....
    }

Here is what the folder looks like :

1 subfolder, 1 zip (1,63 Go), 1 Xlsx (200Kb) !

Please note :

. Same credentials work on same folder when used by other process (code in Python)
. Same Java code works on other Folders

enter image description here

CodePudding user response:

If you're getting a 200 response with an empty list and you know that the folder has some files, then the most likely reason is that the folder is not shared with the service account.

From what I see in your code, when setting up the credentials you used createDelegated(SERVICE_ACCOUNT), where SERVICE_ACCOUNT is just the xxxxxxxxx.iam.gserviceaccount.com address of the service account that you're using. The documentation states that if the service account has domain-wide delegation you have to specify the user that you want to impersonate (so if the folder is owned by [email protected], you have to use that address instead):

If the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.

So, if you've already set up domain-wide delegation for your service account you need to first impersonate the owner of the folder, or someone else who has access to it. Otherwise you need to first share the folder with your service account.

References:

CodePudding user response:

A service account is a dummy user, it has its own google drive account. The reason its returning no files is that you have not uploaded any yet.

If you have a directory on your own personal google drive account, you can take the service account email address and share the directory with the service account. The service account will then have access to any files within that directory.

If you have a google workspace account, you could also configure domain wide delegation and allow the service account to access anything on the workspace domain.

  • Related