Files
DiunaBI/src/Backend/DiunaBI.Infrastructure/Services/GoogleSheetsHelper.cs

35 lines
1.1 KiB
C#
Raw Normal View History

2025-11-05 20:50:25 +01:00
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
2025-11-05 20:50:25 +01:00
namespace DiunaBI.Infrastructure.Services;
2025-11-05 20:50:25 +01:00
public class GoogleSheetsHelper
{
public SheetsService? Service { get; private set; }
private const string ApplicationName = "Diuna";
private static readonly string[] Scopes = [SheetsService.Scope.Spreadsheets];
public GoogleSheetsHelper()
{
InitializeService();
}
private void InitializeService()
{
var credential = GetCredentialsFromFile();
Service = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
}
private static GoogleCredential GetCredentialsFromFile()
{
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;
}
}