Welcome to the new Box Support website. Check out all the details here on what’s changed.

JWT service account: how to generate access tokens for individual user accounts

New post

Comments

4 comments

  • kendomen

    Here's a java example for getting a managed user. 

     

    public static void main(String[] args) throws Exception {
    
            File keyFile = new File(PRIVATE_KEY_FILE);
            byte[] fileData = new byte[(int) keyFile.length()];
            DataInputStream dis = new DataInputStream(new FileInputStream(keyFile));
            dis.readFully(fileData);
            dis.close();
    
            String privateKey = new String(fileData);
    
            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);
    
            BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
            System.out.format("Welcome, %s!\n\n", userInfo.getName());
    
            Iterable managedUsers = BoxUser.getAllEnterpriseUsers(api, "ken.domen@nike.com");
            for (BoxUser.Info managedUser : managedUsers) {
                System.out.println(managedUser.getName() + " " + managedUser.getStatus());
                if (managedUser.getStatus().equals(BoxUser.Status.ACTIVE)) {
    
                    // BoxDeveloperEditionAPIConnection. getAppUserConnection() is used to get AppUser or ManagedUser
                    // in this example, I'm getting a managedUser (ken.domen@nike.com)
                    BoxDeveloperEditionAPIConnection userApi = BoxDeveloperEditionAPIConnection.getAppUserConnection(managedUser.getID(), CLIENT_ID, CLIENT_SECRET, encryptionPref, accessTokenCache);
    
                    BoxFolder boxFolder = new BoxFolder(userApi, "0");
                    Iterable items = boxFolder.getChildren();
                    for (BoxItem.Info item : items) {
                        if (item instanceof BoxFile.Info) {
                            BoxFile.Info fileInto = (BoxFile.Info) item;
                            System.out.println("\t" + item.getName());
                        }
                    }
                }
            }
        }

     

    0
    Comment actions Permalink
  • dtheodor

    Um ok. I suppose if I dig into the Java SDK implementation I will find out how to construct the appropriate HTTP calls? And this snippet gets a personal user token out of a service account token right?

    0
    Comment actions Permalink
  • camporesi

    I'm running the same code and it works for all the AppUser, but returns error 400 for the managed Users.

     

    JAvadoc says that method   getAppUserConnection is for AppUser only.   

     

    Should it work also for managed users ?

     

    0
    Comment actions Permalink
  • kendomen

    Yes it works for managed users as well.  Do you have "Perform Actions as Users" enabled? 

    0
    Comment actions Permalink

Please sign in to leave a comment.