Click here to Skip to main content
15,438,095 members
Articles / Programming Languages / Visual Basic
Posted 7 May 2014

Tagged as


4 bookmarked

Uploading files using Box API

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
7 May 2014CPOL2 min read
Guide to upload files to Box using Box API

Download sample


Following article shows you to how to interact with Box API and upload files from your PC to Box platform.

Using the code

Box platform enables you to upload files and share. This article guides you how to upload files diagrammatically to Box. Before start you need to create an Box account. Go to and create an application. Once you create the application, you will get the client Id and client secret as OAuth2 parameters. Refer below image,

Image 1

Install the Box version 2 dll from Nuget. From the Visual Studio, Open the Package Manager Console. (Tools>Nuget Package Manager>Package Manager Console).

Install-Package Box.v2.SDK  

Once installed, necessary dll files will be placed on bin folder.

Refer followings

Imports BoxApi.V2
Imports BoxApi.V2.Authentication.OAuth2
Imports BoxApi.V2.Model
Imports System.IO 

Create a function to upload to box, here uploaded document is converted to stream. BoxApiRefreshToken.txt is added to the project to keep the refresh token and write the new one.

Function UploadToBox(ByVal attachedFilename As String, ByVal stream As System.IO.Stream) As Boolean

       Dim clientID As String
       Dim clientSecret As String
       Dim oldRefreshToken As String
       Dim newToken As BoxApi.V2.Authentication.OAuth2.OAuthToken

       clientID = "your client id"
       clientSecret = "you client secret"

       Dim tokenProvider As New TokenProvider(clientID, clientSecret)

       '''' Reading Refresh token from the file
       Dim streamReader As StreamReader
       streamReader = System.IO.File.OpenText(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))
       oldRefreshToken = streamReader.ReadToEnd()

       newToken = tokenProvider.RefreshAccessToken(oldRefreshToken)
       Dim boxManager As New BoxManager(newToken.AccessToken)

       '''' Writing the new Refresh token to the file
       Dim streamWriter As New StreamWriter(Server.MapPath("~\\Box\\BoxApiRefreshToken.txt"))

       Dim rootFolder As Folder

       rootFolder = boxManager.GetFolder(Folder.Root)

       boxManager.CreateFile(rootFolder, attachedFilename, ConvertStreamToByteArray(stream))

       Return True

   End Function

Create another function to convert the stream to byte array

Private Function ConvertStreamToByteArray(ByVal stream As System.IO.Stream) As Byte()

    Dim streamLength As Long = Convert.ToInt64(stream.Length)
    Dim fileData As Byte() = New Byte(streamLength) {}
    stream.Position = 0
    stream.Read(fileData, 0, streamLength)

    Return fileData

End Function

On the button click, here uploaded file is renamed by adding hash text to the file name.

Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
     If (fuBox.HasFile) Then

         Dim fileName As String
         Dim currentFileStream As System.IO.Stream
         currentFileStream = fuBox.PostedFile.InputStream

         fileName = System.IO.Path.GetFileNameWithoutExtension(fuBox.FileName)
         fileName = fileName + "-" + GetHashCode.ToString + System.IO.Path.GetExtension(fuBox.FileName)

         Me.UploadToBox(fileName, currentFileStream)


     End If
 End Sub

Point of Interest

Before running the application you need to manually add the refresh token to the text file for the first time. Once added initially new refresh token is written to the text file by the application. Following steps shows how to get the refresh token manually.

Step 1 

Type above url on the browser, replace client_id (xxx) with your own, you will prompt the log in window. Type Box account log in information and click Authorize. Refer the image below

Image 2

Step 2

Once proceed you will get a code as follows

Image 3

Step 3

Install Postman Extension (RestClient) on Chrome browser, Use following URL to query the refresh token ( You have to set client id, client secret and code to get the refresh token. Once you send preferred data you will get access token information from Box API. Please refer following image.

Image 4

Copy the refresh token value to the text file and run the application. if you find difficult in getting token from the Postman, please refer this video.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionThe underlying connection was closed: An unexpected error occurred on a send Pin
mais gharaibeh28-May-19 0:44
Membermais gharaibeh28-May-19 0:44 
QuestionDoes the method for getting the Authorization Code still work? Pin
RayGoforth27-Jun-18 5:01
MemberRayGoforth27-Jun-18 5:01 
QuestionAdding version to the file Pin
jameskm692-May-17 10:36
Memberjameskm692-May-17 10:36 
GeneralThank you Pin
BruceG30-Jan-17 10:52
MemberBruceG30-Jan-17 10:52 
QuestionDownload file from BOX API Pin
Member 1178090924-Nov-15 22:30
MemberMember 1178090924-Nov-15 22:30 
QuestionFile doesn't upload in box Pin
kalsa22-Jul-14 22:57
Memberkalsa22-Jul-14 22:57 
AnswerRe: File doesn't upload in box Pin
Chamila Nishantha23-Jul-14 0:29
MemberChamila Nishantha23-Jul-14 0:29 
GeneralRe: File doesn't upload in box Pin
kalsa23-Jul-14 0:57
Memberkalsa23-Jul-14 0:57 
GeneralRe: File doesn't upload in box Pin
Chamila Nishantha23-Jul-14 15:53
MemberChamila Nishantha23-Jul-14 15:53 
GeneralRe: File doesn't upload in box Pin
Sukku00724-Jun-15 8:02
MemberSukku00724-Jun-15 8:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.