在VB.NET中将文件上传到Google驱动器 - 搜索工作代码

时间:2015-11-30 16:30:20

标签: vb.net visual-studio google-drive-api visual-studio-2015

我想使用vb.net将txt文件上传到我的google驱动器,我正在搜索大约2个小时,我发现这个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

我不能让这个代码工作..有人可以帮我修复这个代码ro post这里工作vb.net代码,我可以只添加我的客户端ID,客户端秘密?

2 个答案:

答案 0 :(得分:2)

以下是一步一步的代码:

1。创建一个控制台VB.NET应用程序。

2。安装NuGet包。

打开查看&gt; 其他Windows &gt; 程序包管理器控制台并键入:

Install-Package Google.Apis.Drive.v2

输出应如下所示:

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> 

3。在Module1.vb中复制并粘贴以下代码:

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

别忘了替换:

  • 要上传的文件路径。
  • 您的客户ID。
  • 您的客户秘密。

在此处获取您的客户ID和客户端密码:https://console.developers.google.com/apis/credentials/oauthclient/

就是这样!您的文件应显示在https://drive.google.com/drive/my-drive

答案 1 :(得分:0)

有效!

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

我不明白&#34;客户ID&#34;但不知怎的,我在尝试了30分钟后完成了它。