Refresh token is over written on each request and store on text file.
Convert stream to byte array
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 = ConfigurationManager.AppSettings("clientid") clientSecret = ConfigurationManager.AppSettings("clientsecret") 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 uploadFolder As Folder ''''rootFolder = boxManager.GetFolder(Folder.Root) uploadFolder = boxManager.GetFolder(ConfigurationManager.AppSettings("uploadFolderID")) Dim fileName As String fileName = System.IO.Path.GetFileNameWithoutExtension(attachedFilename) fileName = fileName + "-" + Guid.NewGuid.ToString + System.IO.Path.GetExtension(attachedFilename) boxManager.CreateFile(uploadFolder, fileName, ConvertStreamToByteArray(stream)) End Function
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
No comments:
Post a Comment