2023-02-22 12:12:38 +01:00
|
|
|
|
using Google.Apis.Auth.OAuth2;
|
|
|
|
|
|
using Google.Apis.Drive.v3;
|
|
|
|
|
|
using Google.Apis.Services;
|
|
|
|
|
|
|
2024-06-18 18:39:02 +02:00
|
|
|
|
namespace WebAPI;
|
|
|
|
|
|
|
|
|
|
|
|
public class GoogleDriveHelper
|
2023-02-22 12:12:38 +01:00
|
|
|
|
{
|
2024-06-18 19:40:16 +02:00
|
|
|
|
public DriveService? Service { get; private set; }
|
2024-06-18 18:39:02 +02:00
|
|
|
|
private const string ApplicationName = "Diuna";
|
2024-06-18 22:24:04 +02:00
|
|
|
|
private static readonly string[] Scopes = [DriveService.Scope.Drive];
|
2024-06-18 18:39:02 +02:00
|
|
|
|
public GoogleDriveHelper()
|
2023-02-22 12:12:38 +01:00
|
|
|
|
{
|
2024-06-18 18:39:02 +02:00
|
|
|
|
InitializeService();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void InitializeService()
|
|
|
|
|
|
{
|
|
|
|
|
|
var credential = GetCredentialsFromFile();
|
|
|
|
|
|
Service = new DriveService(new BaseClientService.Initializer
|
2023-02-22 12:12:38 +01:00
|
|
|
|
{
|
2024-06-18 18:39:02 +02:00
|
|
|
|
HttpClientInitializer = credential,
|
|
|
|
|
|
ApplicationName = ApplicationName
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
private static GoogleCredential GetCredentialsFromFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
// ReSharper disable once RedundantAssignment
|
|
|
|
|
|
var fileName = "client_secrets.json";
|
2023-02-22 12:12:38 +01:00
|
|
|
|
#if DEBUG
|
2024-06-18 18:39:02 +02:00
|
|
|
|
fileName = "client_secrets.Development.json";
|
2023-02-22 12:12:38 +01:00
|
|
|
|
#endif
|
2024-06-18 18:39:02 +02:00
|
|
|
|
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
|
|
|
|
|
|
return credential;
|
2023-02-22 12:12:38 +01:00
|
|
|
|
}
|
2024-06-18 18:39:02 +02:00
|
|
|
|
}
|