Esempio 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 = await tokenExchange.ExchangeAsync();

            _ = CreateClientByToken(token1);

            // Set resource
            tokenExchange.SetResource(resource);
            var token2 = await tokenExchange.ExchangeAsync();

            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);
        }