Box’s new authentication model allows applications to authenticate directly to Box using a JSON Web Token (JWT) signed with an RSA key. This authentication method is meant for server-to-server applications and replaces the first leg of the standard 3-legged OAuth 2.0 process in which users grant an application authorization to access their Box account.
https://docs.box.com/docs/getting-started-box-platform
コード例 #1
0
ファイル: Program.cs プロジェクト: box/box-windows-sdk-v2
        private async Task ExecuteMainAsync()
        {
            var config = ConfigureBoxApi();
            var session = new BoxJWTAuth(config);

            // client with permissions to manage application users
            var adminToken = session.AdminToken();
            var client = session.AdminClient(adminToken);

            var user = await CreateNewUser(client);
            Console.WriteLine("New app user created with Id = {0}", user.Id);

            // user client with access to user's data (folders, files, etc)
            var userToken = session.UserToken(user.Id);
            var userClient = session.UserClient(userToken, user.Id);

            // root folder has id = 0
            var newFolder = await CreateNewFolder(userClient);
            Console.WriteLine("New folder created with Id = {0}", newFolder.Id);

            var timer = Stopwatch.StartNew();
            var file = File.OpenRead("box_logo.png");
            var uploaded = await UploadFile(newFolder, userClient, file);
            Console.WriteLine("New file uploaded with Id = {0} in {1} ms", uploaded.Id, timer.ElapsedMilliseconds);
        }
コード例 #2
0
        /// <summary>
        /// Retrieves a new access token using BoxJWTAuth 
        /// </summary>
        /// <param name="accessToken">This input is not used. Could be set to null</param>
        /// <returns>OAuth session</returns>
        public async Task<OAuthSession> RefreshAccessTokenAsync(string accessToken)
        {
            OAuthSession session = UserId != null
                ? BoxJWTAuth.Session(await BoxJWTAuth.UserTokenAsync(UserId).ConfigureAwait(false))
                : BoxJWTAuth.Session(await BoxJWTAuth.AdminTokenAsync().ConfigureAwait(false));

            Session = session;
            OnSessionAuthenticated(session);

            return session;
        }
コード例 #3
0
        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.pem");

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

            Console.WriteLine("Admin root folder items");
            var items = await adminClient.FoldersManager.GetFolderItemsAsync("0", 500);
            items.Entries.ForEach(i =>
            {
                Console.WriteLine("\t{0}", i.Name);
                //if (i.Type == "file")
                //{
                //    var preview_link = adminClient.FilesManager.GetPreviewLinkAsync(i.Id).Result;
                //    Console.WriteLine("\tPreview Link: {0}", preview_link.ToString());
                //    Console.WriteLine();
                //}   
            });
            Console.WriteLine();

            var userRequest = new BoxUserRequest() { Name = "test appuser", IsPlatformAccessOnly = true };
            var appUser = await adminClient.UsersManager.CreateEnterpriseUserAsync(userRequest);
            Console.WriteLine("Created App User");

            var userToken = boxJWT.UserToken(appUser.Id);
            var userClient = boxJWT.UserClient(userToken, appUser.Id);

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

            await adminClient.UsersManager.DeleteEnterpriseUserAsync(appUser.Id, false, true);
            Console.WriteLine("Deleted App User");
        }
コード例 #4
0
 /// <summary>
 /// Constructor JWT auth. repository
 /// </summary>
 /// <param name="session">OAuth session</param>
 /// <param name="boxJWTAuth">JWT authentication</param>
 /// <param name="userId">Id of the user</param>
 public JWTAuthRepository(OAuthSession session, BoxJWTAuth boxJWTAuth, string userId = null)
 {
     this.Session = session;
     this.BoxJWTAuth = boxJWTAuth;
     this.UserId = userId;
 }
コード例 #5
0
 public JWTAuthRepository(OAuthSession session, BoxJWTAuth boxJWTAuth, string userId = null)
 {
     this.Session    = session;
     this.BoxJWTAuth = boxJWTAuth;
     this.UserId     = userId;
 }
コード例 #6
0
 /// <summary>
 /// Constructor JWT auth. repository
 /// </summary>
 /// <param name="session">OAuth session</param>
 /// <param name="boxJWTAuth">JWT authentication</param>
 /// <param name="userId">Id of the user</param>
 public JWTAuthRepository(OAuthSession session, BoxJWTAuth boxJWTAuth, string userId = null)
 {
     Session = session;
     BoxJWTAuth = boxJWTAuth;
     UserId = userId;
 }