upload files to Google drive in VB.NET - searching

2020-06-25 06:54发布

I want to upload txt files to my google drive using vb.net , i was searching for about a 2 hours and i found this Upload and download to Google Drive using VB.NET Form

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

i cant get this code to work .. can someone help me fix this code ro post here working vb.net code where i can just add my client id , client secret?

2条回答
Luminary・发光体
2楼-- · 2020-06-25 07:29

Here is the code step by step:

1. Create a Console VB.NET app.

2. Install the NuGet package.

Open View > Other Windows > Package Manager Console and type:

Install-Package Google.Apis.Drive.v2

The output should look like:

PM> Install-Package Google.Apis.Drive.v2

...
Adding 'Google.Apis 1.9.2' to YourApp.
Successfully added 'Google.Apis 1.9.2' to YourApp.
Adding 'Google.Apis.Auth 1.9.2' to YourApp.
Successfully added 'Google.Apis.Auth 1.9.2' to YourApp.
Adding 'Google.Apis.Drive.v2 1.9.2.1940' to YourApp.
Successfully added 'Google.Apis.Drive.v2 1.9.2.1940' to YourApp.

PM> 

3. Copy and paste the following code in Module1.vb:

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

' Your original code was missing the following "Imports":
Imports Google.Apis.Drive.v2
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports System.Threading
Imports Google.Apis.Drive.v2.Data

Module Module1

    ' Call UploadFile(...) from your programs Main():
    Sub Main()
        UploadFile("C:\file_to_upload.txt")
    End Sub

    Private Service As DriveService = New DriveService

    Private Sub CreateService()
        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 Sub

    Private Sub UploadFile(FilePath As String)
        'Not needed from a Console app:
        '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)

        '' You were mmissing the Upload part!
        UploadRequest.Upload()
        Dim file As File = UploadRequest.ResponseBody

        ' Not needed from a Console app:
        'Me.Cursor = Cursors.Default

        MsgBox("Upload Finished")
    End Sub
End Module

Do not forget to replace:

  • Path of file to upload.
  • Your client ID.
  • Your client secret.

Get your client ID and client secret here: https://console.developers.google.com/apis/credentials/oauthclient/

And that's it! Your file should appear on https://drive.google.com/drive/my-drive

查看更多
ら.Afraid
3楼-- · 2020-06-25 07:52

It works!

TheFile.Title = "My document.txt"'I added extension.
    TheFile.Description = "A test document"
    TheFile.MimeType = ""'I left it blank,because type has been added before

I don't understand "client ID" but somehow I finished it after 30minutes of trying.

查看更多
登录 后发表回答