Nicholas DiPiazza
Nicholas DiPiazza

Reputation: 10595

Box.com - How to use a service account to crawl all enterprise users' box files

I am using Box.com Java SDK.

I'd like to loop through the users in my Box.com enterprise and crawl the documents in each persons account.

I have set Authentication type set up for OAuth2.0 with JWT. I have enabled the "As-User" header. and I have updated "User Access" to "All Users" so that I can see all users files whether they are through the https://box.com website or if they are completely on the backend.

If I request all the files in /0 as this user, will it return me all the files for all users on the system?

How can I start crawling through each users file?

Upvotes: 0

Views: 735

Answers (1)

kendomen
kendomen

Reputation: 778

I tried this and it seems to work...

public static void main(String[] args) throws IOException {
{
        String privateKey = new String(Files.readAllBytes(Paths.get(PRIVATE_KEY_FILE)));

        JWTEncryptionPreferences encryptionPref = new JWTEncryptionPreferences();
        encryptionPref.setPublicKeyID(PUBLIC_KEY_ID);
        encryptionPref.setPrivateKey(privateKey);
        encryptionPref.setPrivateKeyPassword(PRIVATE_KEY_PASSWORD);
        encryptionPref.setEncryptionAlgorithm(EncryptionAlgorithm.RSA_SHA_256);

        IAccessTokenCache accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES);

        BoxDeveloperEditionAPIConnection api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(ENTERPRISE_ID, CLIENT_ID, CLIENT_SECRET, encryptionPref, accessTokenCache);

        Iterable<com.box.sdk.BoxUser.Info> users = BoxUser.getAllEnterpriseUsers(api, "App");
        for (BoxUser.Info user : users) {

            BoxDeveloperEditionAPIConnection userApi = BoxDeveloperEditionAPIConnection.getAppUserConnection(user.getID(), CLIENT_ID, CLIENT_SECRET, encryptionPref, accessTokenCache);
            System.out.println(new BoxUser(userApi, user.getID()).getInfo().getName());

            BoxFolder boxFolder = BoxFolder.getRootFolder(userApi);
            Iterable<com.box.sdk.BoxItem.Info> items = boxFolder.getChildren();
            for (BoxItem.Info item : items) {
                System.out.println("\t" + item.getName());
            }
        }
}

Upvotes: 1

Related Questions