Ciao a tutti, sto cercando di capire come uploadare un file su google drive e cercando su google ho trovato come fare, solo che con il codice che ho trovato, modificato con i miei dati, VS2022 mi restituisce, sulla riga “Dim request As PermissionsResource.CreateRequest = moService.Permissions.Create(permission, oFile.Id)” l'errore: " System.NullReferenceException HResult=0x80004003 Messaggio=Riferimento a un oggetto non impostato su un'istanza di oggetto"
Mi aiutate a risolvere? Specifico che ho installato, tramite Nuget, Google API v3 e ho messo i dati corretti su ClientID e ClientSecret (omessi per privacy):
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 = "xxxxxxxxxxxxxxxxxxxxxxxxx"
Dim ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx"
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
EDIT: scusate, dopo giorni che ci provavo ho capito l'errore subito dopo aver postato la domanda. Lascio tutto perchè magari potrà servire a qualcuno…il codice era corretto, dovevo solo abilitare l'API dal Google Cloud Console