How to use Box Service Account - Part 2 - Microsoft Technologies, Azure and .net Tutorials

Latest

Saturday, March 28, 2020

How to use Box Service Account - Part 2

 

Create account in Auth0

  • We have to create account in Auth0 for jwt token.
  • Register an application
  • For this open Auth0 link. if you have already an account then login otherwise click on Sign up. Use the link https://auth0.com/docs/login
  • Fill the email id and password
  • Click on Sign Up
  • You will get the following screen
  • Select the region
  • Click on Next

  • If you want to create personal account then click on Personal other wise click on Company. Here I am selecting personal.
  • Fill the necessary details.
  • After selecting personal you will get the following screen.
  • Click on Create Account
  • After that you will get the following screen.
  • On the left side click on Applications

  • You will get the following screen
  • Click on Create Application
  • You will get the following screen.
  • Select the Machine to machine applications
  • Click on Create
  • You will get the following screen.
  • Select the API
  • Select the Scopes
  • Here I am selecting All
  • Click on Authorize
  • After that you will get the following screen.
  • Here we have lot of option to choose programming language of our choice.
  • We are selecting C# as our language
  • Click on C# and use that code for getting the token that is used for authenticating the Box

  • Now that is it we have completed auth0 part.
  • Now we are go to visual studio and create new project
  • Open visual studio 2019
  • Create a new windows forms project using c#
  • From nugget package install the Box.V2 and Restsharp


  • Add new Folder BoxConfigFile and copy the box configuration file in that you have downloaded and rename it to boxConfig.json

  • Add new class BoxAPIToken this is used to serialize the response return from Auth0
      public class BoxAPIToken
    {
        public string access_token { get; set; }
        public string expires_in { get; set; }
        public string token_type { get; set; }
    }
  • Add New class BoxJwt
   

    public class BoxJwt
    {
        private IBoxConfig boxConfig;
        public BoxJWTAuth BOX_JWT_HELPER;
        public BoxClient BoxUserClient;
        public string access_token;

        public BoxJwt(string boxconfig)
        {
            try
            {
                boxConfig = BoxConfig.CreateFromJsonString(boxconfig);

                BOX_JWT_HELPER = new BoxJWTAuth(boxConfig);
            }
            catch (Exception ex)
            {

            }
        }

        public BoxClient GetBoxUserClient(string boxUserId = null)
        {
            BoxClient boxClient = null;
            try
            {
  

                         //Copy the code from Auth0 mentioned below


            var token = JsonConvert.DeserializeObject<BoxAPIToken>                                        (response.Content);            

                if (string.IsNullOrEmpty(boxUserId)) boxUserId = null;

                boxClient = BOX_JWT_HELPER.UserClient(token.access_token,                                     boxUserId);
            }
            catch (Exception ex)
            {

            }
            return boxClient;
        }
  • On the windows form add 2 button
  • Create Folder button
  • Upload File button
  • On the form load do the following code

        BoxClient boxClient=null;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string appPath =                                                   Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                appPath = Path.Combine(appPath, "BoxConfigFile");
                appPath = appPath + "\\boxConfig.json";

                string jsonConfig = File.ReadAllText(appPath);

                BoxJwt boxJwt = new BoxJwt(jsonConfig);
                boxClient = boxJwt.GetBoxUserClient();
            }
            catch(Exception ex)
            {

            }
        }


  • Add the following function in the form

        public async Task CreateFolder()
        {
            try
            {
                // Create a new folder in the user's root folder
                var folderParams = new BoxFolderRequest()
                {
                    Name = "Demo",
                    Parent = new BoxRequestEntity()
                    {
                        Id = "0"
                    }
                };

                BoxFolder folder = await boxClient.FoldersManager.CreateAsync(folderParams);

            }
            catch (Exception ex)
            {

            }
        }
  • On the click event button of Create Folder, do the following code

        private void CmdCreateFolder_Click(object sender, EventArgs e)
        {
            try
            {
                CreateFolder().Wait();
            }
            catch(Exception ex)
            {

            }
        }

  • Run the application
  • Click on Create Folder button, Demo folder is created in the application context of Box.
  • To see the Folder Id, that is created in application Context of Box, Login to your account
  • Click on Admin Console
  • Select the application that you have created.
  • Hover on the Demo folder, you will see the folder Id in left bottom corner
  • Below is the screen
    • Add new function in the form code

    public async Task UploadFile()
            {
                try
                {
                    BoxFile newFile;

                    // Create request object with name and parent folder the file should be uploaded to
                    using (FileStream stream = new FileStream(@"D:\\example.pdf", FileMode.Open))
                    {
                        BoxFileRequest req = new BoxFileRequest()
                        {
                            Name = "example.pdf",
                            Parent = new BoxRequestEntity() { Id = "106108558600" }
                        };
                        newFile = await boxClient.FilesManager.UploadAsync(req, stream);
                    }
                }
                catch (Exception ex)
                {

                }
            }


    • In the parent provide the id of Demo folder in which file will be uploaded.
    • On the click event of Upload File, do the following code.
             private void CmdUploadFile_Click(object sender, EventArgs e)
              {
                  try
                  {
                      UploadFile().Wait();
                  }
                  catch(Exception ex)
                  {

                  }
              }
      • Run the Application
      • Click on button Upload File, then file will be uploaded in Demo folder
      • To see that login into your Box Account.
      • Go to admin console
      • After login you will get the following screen
        • Click on Admin Console
        • You will get the following screen
          • Click on the Content on the left side
          • You will get the following screen.
            • Click on the application that you have created
            • You will see the Demo folder that you have created by code

              • Click on the Demo folder, you will see the file that you have uploaded



              No comments:

              Post a Comment

              Metadata in BoxDicom

                Metadata in BoxDicom In BoxDICOM, metadata refers to additional information that can be associated with a file or folder. Metadata in Bo...