none
Google Drive API in visual basic

    Question

  • I've been trying to find out how to get the Google Drive API to work in visual basic. I'm wanting to use this code for my notepad program. I just want it to upload the notes after pushing the upload button on my form. I've also found other pages showing how to do it, but there code didn't work. Here is the API page [LINK], but it doesn't show code for VB.  I've been messing around with code and got this..

    Imports Google.Apis.Drive.v3
    Imports Google.Apis.Auth.OAuth2
    Imports Google.Apis.Drive.v3.Data
    Imports Google.Apis.Services
    Imports System.Threading
    
    
    Public Class Form1
    
        Private Service As DriveService = New DriveService
    
        Private Sub CreateService()
            Dim ClientId = "Client ID"
            Dim ClientSecret = "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)
            Dim TheFile As New File()
            TheFile.Name = TextBox3.Text
            TheFile.Description = "Google Drive API Test Program"
            TheFile.MimeType = "text/plain"
        End Sub
    End Class
    

    .... I'm not sure where to go from here.. thanks so much. :D (I'm using Google.Apis.Drive.v3 from NuGet btw)

    Sunday, April 30, 2017 6:55 PM

All replies

  • BUMP
    Monday, May 1, 2017 10:02 PM
  • Hi TimeFluid,

    According to your description, you want to upload a file by using Google Drive API. I do some code you can refer to.

     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)
            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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filename As String = "D:/"
            CreateService()
            UploadFile(filename)
        End Sub

    Hope it is helpful to you.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 8:10 AM
    Moderator
  • Thank you for your message back. I still get some errors tho. :(

    Tuesday, May 2, 2017 11:38 PM
  • Hi TimeFluid,

    Have you download Google.Apis.Drives.v2 dll? you need to download this and imports some namespaces.

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

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 4, 2017 5:51 AM
    Moderator