Box.com API使用 - 获取文件夹计数作为服务应用程序

时间:2017-03-02 07:02:45

标签: box

我们正在创建一个与您系统中的服务帐户一起使用的应用程序;另一个用户(user-2)通过将我们的应用程序密钥添加到其自定义应用程序列表来授权此应用程序。如何获取此User-2的UserID,以便我们可以冒充他并访问他的文件列表和文件等。我们需要他们的UserID,因此我们可以通过" AS-User:&#34 ;头。并且可以使用.NET SDK中的某个属性设置此标头 - 将会感谢示例代码。

1 个答案:

答案 0 :(得分:0)

这适用于所有企业用户,但您可以轻松地使用if语句来获取您正在寻找的用户。

static async Task MainAsync()
    {
        // rename the private_key.pem.example to private_key.pem and put your JWT private key in the file
        var privateKey = File.ReadAllText(PRIVATE_KEY_FILE);

        var boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, privateKey, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID);
        var boxJWT = new BoxJWTAuth(boxConfig);

        var adminToken = boxJWT.AdminToken();
        Console.WriteLine("Admin Token: " + adminToken);
        Console.WriteLine();

        var adminClient = boxJWT.AdminClient(adminToken);  // adminClient == serviceAccount

        var userDetails = await adminClient.UsersManager.GetCurrentUserInformationAsync();
        Console.WriteLine("\tAdmin User Details:");
        Console.WriteLine("\tId: {0}", userDetails.Id);
        Console.WriteLine("\tName: {0}", userDetails.Name);
        Console.WriteLine("\tStatus: {0}", userDetails.Status);
        Console.WriteLine();

        var users = await adminClient.UsersManager.GetEnterpriseUsersAsync();
        users.Entries.ForEach(i =>
        {
            Console.WriteLine("\t{0}", i.Name);
            Console.WriteLine("\t{0}", i.Status);

            if (i.Status == "active")
            {
                var userToken = boxJWT.UserToken(i.Id);
                var userClient = boxJWT.UserClient(userToken, i.Id);

                Task u = getUserItems(userClient, i.Id);
                u.Wait();                                       
            }

        });
    }

    static async Task getUserItems(BoxClient userClient, string id)
    {
        var userDetails = await userClient.UsersManager.GetCurrentUserInformationAsync();
        Console.WriteLine("\nManaged User Details:");
        Console.WriteLine("\tId: {0}", userDetails.Id);
        Console.WriteLine("\tName: {0}", userDetails.Name);
        Console.WriteLine("\tStatus: {0}", userDetails.Status);
        Console.WriteLine();

        Console.WriteLine("managed users older items");
        var items = await userClient.FoldersManager.GetFolderItemsAsync("0", 500);
        items.Entries.ForEach(i =>
        {
            Console.WriteLine("\t{0}", i.Name);
        });
        Console.WriteLine();
    }