2016-04-13 3 views
0

Ich habe Probleme mit Daten von Google Drive.Google REST API herunterladen

Ich verwende Google Drive API v3 mit diesem Code:

Imports System.Security.Cryptography.X509Certificates 
Imports System 
Imports System.Threading 
Imports System.Threading.Tasks 
Imports System.IO 
Imports System.Linq 
Imports System.Text 

Imports Google.Apis.Auth.OAuth2 
Imports Google.Apis.Drive.v3 
Imports Google.Apis.Drive.v3.Data 
Imports Google.Apis.Services 
Imports Google.Apis.Util.Store 


Public Class DownloadMe 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    End Sub 

    Protected Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click 
     DownloadMe() 
    End Sub 

    Protected Sub DownloadMe() 
     Dim fileID As String = "someFileID" 
     Dim driveService As New DriveService 
     Dim request = driveService.Files.Export(fileID, "text/plain") 
     'Dim request = driveService.Files.Get(fileID) 

     Dim dir As String = "C:\Users\JohnDoe\Documents\Visual Studio 2012\Projects\GoogleDriveDownload\" 
     Dim output As String = dir + "test.txt" 
     Dim stream As New System.IO.MemoryStream 

     Try 
      request.Download(stream) 
      If System.IO.File.Exists(output) Then 
       System.IO.File.Delete(output) 
      End If 

      Dim fs As New FileStream(output, FileMode.Create, FileAccess.Write) 
      stream.WriteTo(fs) 

      stream.Close() 
      fs.Close() 

     Catch ex As Exception 
      MyCompany.Diagnostics.Log("An error occured: " + ex.Message) 
      lit0.Text = "An error occured: " + ex.Message + "<br />" 
     End Try 

    End Sub 
End Class 

Das Problem ist ich nichts in den Strom immer bin, so die erstellte „text.txt“ Datei ist eine leere Datei anstelle der typischen "Lorem ipsum ..." Text.

Irgendwelche Hinweise, wie Sie das beheben können?

Antwort

0
Dim Service3 = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = "Google Drive VB Dot Net"}) 

Dim fileId = DataGridView1.CurrentRow.Cells(1).Value 
Dim request = Service3.Files.Get(fileId) 
Dim Results = request.Execute 

'Dim Stream2 = New System.IO.MemoryStream() 
Dim Filename = "C:\Users\" + Environment.UserName + "\Downloads\" & Results.Name 
Using FileStream = New System.IO.FileStream(Filename, System.IO.FileMode.Create, System.IO.FileAccess.Write) 
    request.Download(FileStream) 
End Using