【问题标题】:Google Drive with VBnet Express带有 VBnet Express 的 Google 云端硬盘
【发布时间】:2015-11-04 13:05:45
【问题描述】:

我在使用 VB.net Express 2012 连接到我的 Google 云端硬盘时遇到问题。 支持吗?

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace DriveQuickstart
    Class Program
        Shared Scopes As String() = {DriveService.Scope.DriveReadonly}
        Shared ApplicationName As String = "Drive API .NET Quickstart"

        Private Shared Sub Main(args As String())
            Dim credential As UserCredential

            Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
                Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
                credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart")

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
                Console.WriteLine(Convert.ToString("Credential file saved to: ") & credPath)
            End Using

            ' Create Drive API service.
            Dim service = New DriveService(New BaseClientService.Initializer() With { _
                Key .HttpClientInitializer = credential, _
                Key .ApplicationName = ApplicationName _
            })

            ' Define parameters of request.
            Dim listRequest As FilesResource.ListRequest = service.Files.List()
            listRequest.MaxResults = 10

            ' List files.
            Dim files As IList(Of Google.Apis.Drive.v2.Data.File) = listRequest.Execute().Items
            Console.WriteLine("Files:")
            If files IsNot Nothing AndAlso files.Count > 0 Then
                For Each file As var In files
                    Console.WriteLine("{0} ({1})", file.Title, file.Id)
                Next
            Else
                Console.WriteLine("No files found.")
            End If
            Console.Read()

        End Sub
    End Class
End Namespace

我在控制台中运行 Nuget 失败了,所以我不得不手动将 DLL 复制到项目中并将它们添加为引用,这似乎可以工作,因为 Imports google.* 不再给出错误,但我在代码。

有什么帮助吗?

更新

我认为问题可能是我使用的是 vbnet express 2012 正如https://developers.google.com/drive/web/quickstart/dotnet 所说: 先决条件

要运行此快速入门,您需要:

Visual Studio 2013 或更高版本。

有什么解决办法吗?

更新2

我不再需要此代码,因为我将使用不同的方法 如果其他人有同样的问题,请随时回答

【问题讨论】:

  • 我在 VS 2010 中使用过 .net 客户端库,所以这不是真的。我知道的唯一先决条件是它需要是 .net framework 4.0 或 4.5。
  • 你究竟导入了哪个 nugget 包?
  • portable-net40+sl50+win+wpa81+wp80 的所有 4 个 dll 文件(Google.Apis.Auth.dll、Google.Apis.Core.dll、Google.Apis.dll、Google.Apis .Drive.v2.dll)
  • 这就是你需要导入的全部nuget.org/packages/Google.Apis.Drive.v2 如果导入失败,请确保项目设置为 .net framework 4.0 或 4.5
  • 感谢您的帮助。 :)

标签: vb.net google-api google-drive-api google-api-dotnet-client


【解决方案1】:

对于 var 错误,看起来像 c# 。 将其替换为 Google.Apis.Drive.v2.Data.File 或可能只是 File,因为它已导入。

将 Filedatastore 替换为 Google.Apis.Util.Store.FileDataStore

您没有正确初始化“服务”。这看起来更像是 c#。 使用类似的东西: 像新的一样昏暗的服务... 见:https://msdn.microsoft.com/en-us/library/bb531244.aspx

【讨论】:

    【解决方案2】:
        Imports Google.Apis.Auth.OAuth2
        Imports Google.Apis.Drive.v2
        Imports Google.Apis.Drive.v2.Data
        Imports Google.Apis.Services
        
        Public Class genel
         Dim Service As DriveService = New DriveService
         Dim UploadRequest As FilesResource.InsertMediaUpload
         Dim wrtr As System.IO.StreamWriter
            Public Sub CreateService(cloudid As String, cloudsecret As String)
        
                Dim ClientId = cloudid
                Dim ClientSecret = cloudsecret
                Dim MyUserCredential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "XXX", CancellationToken.None).Result
                Service = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "XXXXXXX"})
            End Sub
        
        Public Function UploadFile(FilePath As String)
        
                Dim TheFile As New Data.File()
        
                TheFile.Title = IO.Path.GetFileName(FilePath)
                TheFile.Description = "Uploaded File From My Data"
                If sonayir = False Then TheFile.Parents = New List(Of ParentReference) From {New ParentReference() With {.Id = folderID}}
        
                Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
                Dim Stream As New System.IO.MemoryStream(ByteArray)
        
                Try
                    UploadRequest = Service.Files.Insert(TheFile, Stream, TheFile.MimeType)
                    UploadRequest.Upload()
        
                    Stream.Close()
                    Return snc
                Catch ex As Exception
                    Return ex.Message
                End Try
            End Function
            Function CreateFolder(folder As String)
                Try
                    Dim dosya As File = New File()
                    dosya.Title = folder
                    dosya.MimeType = "application/vnd.google-apps.folder"
        
                    Dim folder_create = Service.Files.Insert(dosya)
                    Dim snc = folder_create.Execute()
                    Return snc.Id.ToString
                Catch ex As Exception
                    Return ex.Message
                End Try
            End Function
            Function DeleteFileorFolder(id As String)
                Dim snc
                Try
                    snc = Service.Files.Delete(id).Execute()
                Catch ex As Exception
                    Return ex.Message
                End Try
                Return snc
            End Function
            Sub TrashEmpty()
                Try
                    Dim mn = Service.Files.EmptyTrash 
                    mn.Execute()
                Catch ex As Exception
                MsgBox(ex.Message)
                End Try
            End Sub
    End Class
    

    使用;

    Dim gn As New genel
    

    创建文件夹

    folderId = gn.CreateFolder("FolderName")
    

    文件上传

    fileId = gn.UploadFile("FileName") 
    

    删除文件或文件夹

    gn.DeleteFileorFolder(folderId)
    

    垃圾清空

    gn.TrashEmpty()
    

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多