Hi, I use/made this code to Upload, works fine but its in VB, sadly I dont know C# but you can use a translator to convert the code to c#.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = "THE DIRECTORY PATH"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Secrets = New ClientSecrets()
Secrets.ClientId = "CLIENT ID HERE"
Secrets.ClientSecret = "CLIENT SECRET HERE"
Dim scope = New List(Of String)
scope.Add(DriveService.Scope.Drive)
Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "USER", CancellationToken.None).Result()
Dim initializer = New BaseClientService.Initializer
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "APPLICATION NAME"
Dim service = New DriveService(initializer)
Dim body = New File
body.Title = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
body.Description = "DESCRIPTION"
Select Case System.IO.Path.GetExtension(OpenFileDialog1.FileName)
Case ".avi"
body.MimeType = "video/x-msvideo"
Case ".css"
body.MimeType = "text/css"
Case ".doc"
body.MimeType = "application/msword"
Case ".htm", ".html"
body.MimeType = "text/html"
Case ".bmp"
body.MimeType = "image/bmp"
Case ".gif"
body.MimeType = "image/gif"
Case ".jpeg"
body.MimeType = "image/jpeg"
Case ".jpg"
body.MimeType = "image/jpeg"
Case ".docx"
body.MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Case ".pdf"
body.MimeType = "application/pdf"
Case ".ppt"
body.MimeType = "application/vnd.ms-powerpoint"
Case ".pptx"
body.MimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
Case ".xls"
body.MimeType = "application/vnd.ms-excel"
Case ".xlsx"
body.MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Case ".txt"
body.MimeType = "text/plain"
Case ".zip"
body.MimeType = "application/zip"
Case ".rar"
body.MimeType = "application/x-rar-compressed"
Case ".mp3"
body.MimeType = "audio/mpeg"
Case ".mp4"
body.MimeType = "video/mp4"
Case ".png"
body.MimeType = "image/png"
Case Else
body.MimeType = "application/octet-stream"
End Select
Dim byteArray = System.IO.File.ReadAllBytes(OpenFileDialog1.FileName)
Dim stream = New System.IO.MemoryStream(byteArray)
Dim request = service.Files.Insert(body, stream, body.MimeType)
request.Upload()
body = request.ResponseBody
MessageBox.Show("The File id is: " & body.Id, "Subido")
End If
End Sub
Even works to set the mime type.
If it helps you (or anyone) please help me with any working code to use functions of Google Calendar or Contacts or Tasks or Gmail.
There is no much information in VB to work with Google Apis.
Good luck.
Alan Alvarez