Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Uploading files using Box API

0.00/5 (No votes)
7 May 2014 1  
Guide to upload files to Box using Box API

Download sample

Introduction

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 Box.com and create an application. Once you create the application, you will get the client Id and client secret as OAuth2 parameters. Refer below image,

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()
        streamReader.Close()

        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"))
        streamWriter.Write(newToken.RefreshToken)
        streamWriter.Close()

        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)
        stream.Close()

        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)

            currentFileStream.Close()

        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
https://api.box.com/oauth2/authorize?response_type=code&client_id=XXX&state=authenticated 

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

Step 2

Once proceed you will get a code as follows

Step 3

Install Postman Extension (RestClient) on Chrome browser, Use following URL to query the refresh token (https://www.box.com/api/oauth2/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.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here