Upload and download to Google Drive using VB.NET F

2019-02-15 05:45发布

问题:

I am trying to figure out the codes for visual basic .net 2008 of a program (WinForms) that could upload and download some files to and from my google drive account.

Someone can help me?

回答1:

Please look at the .NET samples found on the Google Drive SDK website found below.

Google Drive SDK Documentation

Hope this helps you.



回答2:

It took a long time to find some code that worked and I never did find anything written in vb.net. Everything I found was written for C#. Well after doing a bunch of conversion I was able to get some things working. However it was very little and I had to figure out the rest. So to make other people's life so much easier here is working vb.net code for Drive v2 & OAuth 2.0:

    Imports System.Threading
    Imports System.Threading.Tasks

    Imports Google
    Imports Google.Apis.Auth.OAuth2
    Imports Google.Apis.Drive.v2
    Imports Google.Apis.Drive.v2.Data
    Imports Google.Apis.Services

    Imports Google.Apis.Auth
    Imports Google.Apis.Download

     'Dev Console:
     'https://console.developers.google.com/

     'Nuget command:
     'Install-Package Google.Apis.Drive.v2

    Private Service As DriveService = New DriveService

    Private Sub CreateService()
    If Not BeGreen Then
        Dim ClientId = "your client ID"
        Dim ClientSecret = "your client secret"
        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 If
End Sub


    Private Sub UploadFile(FilePath As String)
    Me.Cursor = Cursors.WaitCursor
    If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()

    Dim TheFile As New File()
    TheFile.Title = "My document"
    TheFile.Description = "A test document"
    TheFile.MimeType = "text/plain"

    Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
    Dim Stream As New System.IO.MemoryStream(ByteArray)

    Dim UploadRequest As FilesResource.InsertMediaUpload = Service.Files.Insert(TheFile, Stream, TheFile.MimeType)

    Me.Cursor = Cursors.Default
    MsgBox("Upload Finished")
End Sub

    Private Sub DownloadFile(url As String, DownloadDir As String)
    Me.Cursor = Cursors.WaitCursor
    If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()

    Dim Downloader = New MediaDownloader(Service)
    Downloader.ChunkSize = 256 * 1024 '256 KB

    ' figure out the right file type base on UploadFileName extension
    Dim Filename = DownloadDir & "NewDoc.txt"
    Using FileStream = New System.IO.FileStream(Filename, System.IO.FileMode.Create, System.IO.FileAccess.Write)
        Dim Progress = Downloader.DownloadAsync(url, FileStream)
        Threading.Thread.Sleep(1000)
        Do While Progress.Status = TaskStatus.Running
        Loop
        If Progress.Status = TaskStatus.RanToCompletion Then
            MsgBox("Downloaded!")
        Else
            MsgBox("Not Downloaded :(")
        End If
    End Using
    Me.Cursor = Cursors.Default
End Sub

If you don't know the URL to download the file, then you can use this code to get one:

    Dim Request = Service.Files.List()
    Request.Q = "mimeType != 'application/vnd.google-apps.folder' and trashed = false"
    Request.MaxResults = 2
    Dim Results = Request.Execute
    For Each Result In Results.Items
        MsgBox(Result.DownloadUrl & vbCrLf & Result.Title & vbCrLf & Result.OriginalFilename)
    Next

I am tired and don't care to clean up the code at the moment, however this works and will get you started. Happy programming!