Files
DiunaBI/WebAPI/GoogleDriveHelper.cs
2024-06-18 22:24:04 +02:00

36 lines
1.1 KiB
C#

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
namespace WebAPI;
public class GoogleDriveHelper
{
public DriveService? Service { get; private set; }
private const string ApplicationName = "Diuna";
private static readonly string[] Scopes = [DriveService.Scope.Drive];
public GoogleDriveHelper()
{
InitializeService();
}
private void InitializeService()
{
var credential = GetCredentialsFromFile();
Service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
}
private static GoogleCredential GetCredentialsFromFile()
{
// ReSharper disable once RedundantAssignment
var fileName = "client_secrets.json";
#if DEBUG
fileName = "client_secrets.Development.json";
#endif
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
var credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
return credential;
}
}