Ejemplo n.º 1
0
        private static async Task MainAsync(string token, string fileId, string folderId)
        {
            var client   = CreateClientByToken(token);
            var fileInfo = await client.FilesManager.GetInformationAsync(fileId);

            Console.WriteLine(string.Format("File name is {0} ", fileInfo.Name));

            // var resource = string.Format("https://api.box.com/2.0/files/{0}", fileId);
            var resource = string.Format("https://api.box.com/2.0/folders/{0}", folderId);

            var scope         = "root_readwrite";
            var tokenExchange = new TokenExchange(token, scope);

            // Check resource to be optional
            var token1  = tokenExchange.Exchange();
            var client1 = CreateClientByToken(token1);

            // Set resource
            tokenExchange.SetResource(resource);
            var token2  = tokenExchange.Exchange();
            var client2 = CreateClientByToken(token2);

            try
            {
                await client2.FilesManager.GetInformationAsync(fileId);
            }
            catch (BoxException exp)
            {
                // The new token does not have access to the file any more.
                Console.WriteLine("Permission denied!");
                Console.WriteLine(exp);
            }

            // Can still access the folder.
            var folderInfo = await client2.FoldersManager.GetInformationAsync(folderId);

            Console.WriteLine(folderInfo.Name);

            /*
             * // Set ActorToken
             * var actorTokenBuilder = new ActorTokenBuilder("FAKE_USER_ID", "YOUR_CLIENT_ID");
             * var actorToken = actorTokenBuilder.build();
             *
             * tokenExchange.setActorToken(actorToken);
             * var tokenWAT1 = tokenExchange.exchange();
             *
             * // Set ActorToken w/ user name
             * actorTokenBuilder.setUserName("uname");
             * actorToken = actorTokenBuilder.build();
             *
             * tokenExchange.setActorToken(actorToken);
             * var tokenWAT2 = tokenExchange.exchange();
             */
        }
        public async Task TokenExchange_LiveSession()
        {
            var token    = _client.Auth.Session.AccessToken;
            var fileId   = "16894965489";
            var folderId = "1927307787";

            var client   = CreateClientByToken(token);
            var fileInfo = await client.FilesManager.GetInformationAsync(fileId);

            // var resource = string.Format("https://api.box.com/2.0/files/{0}", fileId);
            var resource = string.Format("https://api.box.com/2.0/folders/{0}", folderId);

            var scopes = new List <string> {
                "item_preview", "item_delete"
            };
            var tokenExchange = new TokenExchange(token, scopes);

            // Check resource to be optional
            var token1  = tokenExchange.Exchange();
            var client1 = CreateClientByToken(token1);

            // Should be able to access the file
            var file1 = await client1.FilesManager.GetInformationAsync(fileId);

            Assert.IsNotNull(file1.Id);

            // Set resource
            tokenExchange.SetResource(resource);
            var token2  = tokenExchange.Exchange();
            var client2 = CreateClientByToken(token2);

            try
            {
                await client2.FilesManager.GetInformationAsync(fileId);

                Assert.Fail();
            }
            catch (BoxException exp)
            {
                // The new token does not have access to the file any more.
            }

            // Can still access the folder.
            var folderInfo = await client2.FoldersManager.GetInformationAsync(folderId);

            Assert.IsNotNull(folderInfo.Name);
        }
Ejemplo n.º 3
0
        static async Task MainAsync()
        {
            try
            {
                /* Read the config file that is provided when an application is
                 * created in the Box Dev Consolse
                 * */

                string jsonConfig = System.IO.File.ReadAllText(configFile());
                var    config     = BoxConfig.CreateFromJsonString(jsonConfig);
                /* Authenticate. This will provide access to the service account */
                var boxJWT     = new BoxJWTAuth(config);
                var adminToken = "";
                adminToken = boxJWT.AdminToken();
                Console.WriteLine("Admin Token:" + adminToken);

                /*
                 * Searching for a particular user from the enterprise given the login name
                 */
                BoxClient boxClient = boxJWT.AdminClient(adminToken);
                BoxCollection <BoxUser> boxUserCollection = await boxClient.UsersManager.GetEnterpriseUsersAsync(userLogin(), 0, 100, null, "managed", null, false);

                List <BoxUser> userList = boxUserCollection.Entries;
                Console.WriteLine("Entries:" + userList.Count);
                if (userList.Count > 0)
                {
                    foreach (var user in userList)
                    {
                        Console.WriteLine("User Login:"******" ID:" + user.Id);
                    }
                }

                /* Replace this variable for the user you want. This is the users
                 * internal Box ID and is all numbers e.g. 3445252385. Suggest that
                 * the list of users in the system is cached in the Token Factory
                 * and synced perdiodically.
                 */
                var userId = userInformation();
                /* Ask box for a token for the user */
                var userToken = boxJWT.UserToken(userId);

                Console.WriteLine("User Token:" + userToken);
                /* Generate a downscoped token to the ITEM_PREVIEW scope */
                var exchanger = new TokenExchange(adminToken, "item_preview");

                /*Optionally you can downscope to a particular resource. Omitting this will downscope
                 * all resources to the scope set above regardless of resource.
                 * exchanger.SetResource("https://api.box.com/2.0/files/123456789");
                 */
                string downscopedToken = exchanger.Exchange();
                Console.WriteLine("Downscoped ITEM_PREVIEW Token:" + downscopedToken);
                /* Print out some user information for the demo */
                var userClient = boxJWT.UserClient(userToken, userId);

                var userDetails = await userClient.UsersManager.GetCurrentUserInformationAsync();

                Console.WriteLine("\n User Details:");
                Console.WriteLine("\tId: {0}", userDetails.Id);
                Console.WriteLine("\tName: {0}", userDetails.Name);
                Console.WriteLine("\tStatus: {0}", userDetails.Status);
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }