2016-04-13 13 views
0

Googleドライブからデータを取得するのに苦労しています。Google REST APIをダウンロード

私はこのコードでGoogleドライブAPI V3を使用しています:

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 

問題は、私は、ストリームに何を取得していないのですので、作成した「TEXT.TXT」ファイルが空のファイルの代わりに、典型的なものです"Lorem ipsum ..."というテキスト。

これを解決する方法についての手がかりはありますか?

答えて

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 
関連する問題