Search code examples
vb.netfile-uploadgoogle-docs-api

vb.net can't upload my file in google drive


i'm using google drive api for upload a pdf file (with vb.net) , i searched a many tutorial in this and another website (how to get google drive api in vb.net , how to upload ...) and finally i have sucessfully done it but the problem , No files are added in drive

here is my code

Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
    Dim filename As String = fichier.FileName 'from selected file in openfiledialog
    CreateService()
    UploadFile(filename)
End Sub


Private Service As DriveService = New DriveService

Private Sub CreateService()

    Dim ClientId = "*********"
    Dim ClientSecret = "****************"
    Dim MyUserCredential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "user", CancellationToken.None).Result
    Service = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "Google Drive VB Dot Net"})
End Sub
Private Sub UploadFile(FilePath As String)
    Me.Cursor = Cursors.WaitCursor
    CreateService()
    Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
    Dim Stream As New System.IO.MemoryStream(ByteArray)
    Me.Cursor = Cursors.Default
    MsgBox("Upload Finished")
End Sub

when i execute my application , the result is

enter image description here

but i can't found any file in drive


Solution

  • You are calling CreateService() twice but never use it to upload anything.

    Private Sub UploadFile(FilePath As String)
        Me.Cursor = Cursors.WaitCursor
        CreateService()
        Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
        Dim Stream As New System.IO.MemoryStream(ByteArray)
        '' ---------------------------------------------------------
        '' Here you'll going to use the service to upload your data
        '' with Service.Files.Create()
        '' ---------------------------------------------------------
        Me.Cursor = Cursors.Default
        MsgBox("Upload Finished")
    End Sub