我想使用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,客户端秘密?
答案 0 :(得分:2)
以下是一步一步的代码:
打开查看&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>
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和客户端密码: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分钟后完成了它。