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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34003327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:36:20  来源:igfitidea点击:

upload files to Google drive in VB.NET - searching for working code

vb.netvisual-studiogoogle-drive-apivisual-studio-2015

提问by jonas dufek

I want to upload txt files to my google drive using vb.net , i was searching for about a 2 hours and i found this Upload and download to Google Drive using VB.NET Form

我想使用 vb.net 将 txt 文件上传到我的谷歌驱动器,我搜索了大约 2 个小时,我发现这个使用 VB.NET 表单上传并下载到谷歌驱动器

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

i cant get this code to work .. can someone help me fix this code ro post here working vb.net code where i can just add my client id , client secret?

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

回答by kiewic

Here is the code step by step:

这是一步一步的代码:

1. Create a Console VB.NET app.

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

2. Install the NuGet package.

2. 安装 NuGet 包。

Open View> Other Windows> Package Manager Consoleand type:

打开“视图”>“其他窗口”>“程序包管理器控制台”并键入:

Install-Package Google.Apis.Drive.v2

The output should look like:

输出应如下所示:

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. Copy and paste the following code in Module1.vb:

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

Do not forget to replace:

不要忘记更换:

  • Path of file to upload.
  • Your client ID.
  • Your client secret.
  • 要上传的文件路径。
  • 您的客户 ID。
  • 您的客户机密。

Get your client ID and client secret here: https://console.developers.google.com/apis/credentials/oauthclient/

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

And that's it! Your file should appear on https://drive.google.com/drive/my-drive

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

回答by Micha? T

It works!

有用!

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

I don't understand "client ID" but somehow I finished it after 30minutes of trying.

我不明白“客户 ID”,但不知何故,我在尝试了 30 分钟后完成了它。