简体   繁体   中英

Google Drive with VBnet Express

I having problems connecting to my Google Drive using VB.net Express 2012. Does it support it?

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

I ran the Nuget in the console which failed, so I had to copy the DLL's manually to the project and added them as references, which appear to work as the Imports google.* is not giving errors anymore, but I get errors in the code.

在此处输入图像描述

Any help?

Update

I think the problem might be that I am using v.net express 2012 as on the https://developers.google.com/drive/web/quickstart/do.net it says: Prerequisites

To run this quickstart, you'll need:

Visual Studio 2013 or later.

Is there any work around?

Update2

I no longer need this code, as I will be using different method Feel free to answer in case someone else having the same issue

For the var error, that looks like c# . Replace it with Google.Apis.Drive.v2.Data.File or possibly just File since it is imported.

Replace Filedatastore with Google.Apis.Util.Store.FileDataStore

You are not initializing 'service' correctly. Again this looks more like c#. Use something like: Dim service as new... See: https://msdn.microsoft.com/en-us/library/bb531244.aspx

    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

use;

Dim gn As New genel

Create Folder

folderId = gn.CreateFolder("FolderName")

File Upload

fileId = gn.UploadFile("FileName") 

Delete File Or Folder

gn.DeleteFileorFolder(folderId)

Trash Empty

gn.TrashEmpty()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM