将文件上传到 Google Drive 和 VB.net:nullReferenceException 错误

问题描述 投票:0回答:1

我正在尝试学习使用 VB.net 创建的简单软件将文件上传到 Google Drive,但我遇到了一些错误..当我按下 Button1 时,我收到此错误:

“System.NullReferenceException:oFile 没有任何内容”

这一行:

Dim request As PermissionsResource.CreateRequest = moService.Permissions.Create(permission, oFile.Id)

整个代码是:

Imports Google.Apis.Auth
Imports Google.Apis.Download
Imports Google.Apis.Drive.v3
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports System.Threading
Imports Google.Apis.Drive.v3.Data
Imports Google.Apis.Upload

    
   
Public Class Form1
    Private moService As DriveService = New DriveService
    Private msViewLink As String = ""

    Private Sub CreateService()

        Dim ClientId = "yyyyyyyyyyyyyyyyyyyyyyyyyyy"
        Dim ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

        Dim MyUserCredential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "user", CancellationToken.None).Result
        moService = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "GoogleAPI"})

    End Sub

    Private Sub UploadFile()

        ' Checking if the Service is still alive, if not create the service again.
        If moService.ApplicationName <> "GoogleAPI" Then CreateService()

        Dim oGDriveFile As New File()

        oGDriveFile.Name = "prova.pdf" ' Set your File Name here.
        oGDriveFile.Description = "descrizione" ' Set a meaningful description, I had set the same name as my project name
        oGDriveFile.MimeType = "application/pdf" ' You must set your MIME type carefully. Refer to the table above

        Dim bArrByteArray As Byte() = System.IO.File.ReadAllBytes("C:/Users/marco/OneDrive/Desktop/prova.pdf") ' Your File Path from where you would want to upload from.
        Dim oStream As New System.IO.MemoryStream(bArrByteArray)

        Dim oUploadRequest As FilesResource.CreateMediaUpload

        oUploadRequest = moService.Files.Create(oGDriveFile, oStream, oGDriveFile.MimeType)

        oUploadRequest.Fields = "id"
        oUploadRequest.Alt = FilesResource.CreateMediaUpload.AltEnum.Json

        oUploadRequest.Upload()

        Dim oFile As File = oUploadRequest.ResponseBody

        ' Setting this permission will allow anyone having the link to directly download the file. 
        ' Google Drive, will not show any login page to the user, while attempting to download the file.
        ' The below two blocks are imperative.
        Dim permission As New Google.Apis.Drive.v3.Data.Permission()
        permission.Type = "anyone"
        permission.Role = "reader"
        permission.AllowFileDiscovery = True

        Dim request As PermissionsResource.CreateRequest = moService.Permissions.Create(permission, oFile.Id)
        request.Fields = "id"
        request.Execute()

        If Not IsNothing(oFile) Then

            msViewLink = " https://drive.google.com/uc?id=" & oFile.Id
        Else
            Cursor.Current = Cursors.Default
            MessageBox.Show("Unable to contact Google Drive. Check your Connection.", "Title", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Exit Sub
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        UploadFile()
    End Sub
End Class

显然我输入了正确的 clientID 和 clientsecret 并且我已经安装了 Google Apis Drive v3 (请原谅我的英语不好!)

vb.net google-drive-api upload drive
1个回答
0
投票

如何撤销凭证并在每次需要上传文件时要求提供凭证?

© www.soinside.com 2019 - 2024. All rights reserved.