Reputation: 95
I want to upload txt files to my google drive using vb.net , I was searching for about a 2 hours and 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
Can't get this code to work .. can someone help me fix this code or post here other working vb.net code?
Upvotes: 4
Views: 12667
Reputation: 1
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.
Upvotes: 0
Reputation: 16420
Here is the code step by step:
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>
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:
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
Upvotes: 4