VB.NET: Unable to upload PDF to Google Drive

I’m having trouble with my VB.NET app that’s supposed to upload PDFs to Google Drive. I’ve set up the Google Drive API and followed lots of tutorials, but nothing’s showing up in my Drive after I run the program.

Here’s a simplified version of what I’m trying:

Private Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click
    Dim pdfPath As String = FileSelector.FileName
    InitializeGoogleDriveService()
    SendPDFToDrive(pdfPath)
End Sub

Private GDriveService As DriveService = New DriveService

Private Sub InitializeGoogleDriveService()
    Dim appID = "MY_APP_ID"
    Dim appSecret = "MY_APP_SECRET"
    Dim authInfo As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        New ClientSecrets() With {.ClientId = appID, .ClientSecret = appSecret},
        {DriveService.Scope.Drive}, "user", CancellationToken.None).Result
    GDriveService = New DriveService(New BaseClientService.Initializer() With
        {.HttpClientInitializer = authInfo, .ApplicationName = "PDF Uploader"})
End Sub

Private Sub SendPDFToDrive(filePath As String)
    Dim pdfContent As Byte() = System.IO.File.ReadAllBytes(filePath)
    Dim pdfStream As New System.IO.MemoryStream(pdfContent)
    MessageBox.Show("Upload complete")
End Sub

The app runs without errors and shows an “Upload complete” message, but I can’t find any new files in my Google Drive. What am I missing?

I noticed you’re missing a crucial step in your code. The SendPDFToDrive method isn’t actually uploading anything to Google Drive. It’s just reading the file into memory.

To fix this, you need to create a FilesResource.CreateMediaUpload request and execute it. Here’s a quick modification to your SendPDFToDrive method that should work:

Private Sub SendPDFToDrive(filePath As String)
    Dim fileMetadata As New Google.Apis.Drive.v3.Data.File()
    fileMetadata.Name = System.IO.Path.GetFileName(filePath)
    
    Using stream As New System.IO.FileStream(filePath, System.IO.FileMode.Open)
        Dim request As FilesResource.CreateMediaUpload = GDriveService.Files.Create(fileMetadata, stream, 'application/pdf')
        request.Upload()
    End Using

    If request.Status = UploadStatus.Failed Then
        MessageBox.Show('Upload failed: ' & request.Exception.Message)
    Else
        MessageBox.Show('Upload complete')
    End If
End Sub

This should actually upload your PDF to Google Drive. Also, double-check your API credentials and make sure they have the correct permissions.

I’ve encountered similar issues when working with Google Drive API in VB.NET. One crucial step you’re missing is actually executing the upload request. Your ‘SendPDFToDrive’ method creates a MemoryStream but doesn’t send it anywhere.

Try modifying your method like this:

Private Sub SendPDFToDrive(filePath As String)
    Dim fileInfo As New System.IO.FileInfo(filePath)
    Dim uploadStream As System.IO.FileStream = fileInfo.Open(System.IO.FileMode.Open)

    Dim fileMetadata As New Google.Apis.Drive.v3.Data.File()
    fileMetadata.Name = System.IO.Path.GetFileName(filePath)

    Dim request As FilesResource.CreateMediaUpload = GDriveService.Files.Create(fileMetadata, uploadStream, "application/pdf")
    request.Upload()

    If request.Status = UploadStatus.Failed Then
        Console.WriteLine(request.Exception.Message)
    End If

    MessageBox.Show("Upload complete")
End Sub

This should actually send the file to Google Drive. Also, make sure your Google Drive API credentials are correct and have the necessary permissions. Hope this helps!

hey mate, looks like ur missing the actual upload bit in ur code. try adding this to ur SendPDFToDrive method:

Dim fileMetadata As New Google.Apis.Drive.v3.Data.File()
fileMetadata.Name = System.IO.Path.GetFileName(filePath)
Dim request = GDriveService.Files.Create(fileMetadata, pdfStream, ‘application/pdf’)
request.Upload()

that should do the trick! lmk if u need more help