Files
DiunaBI/WebAPI/GoogleSheetsHelper.cs

36 lines
1.1 KiB
C#
Raw Normal View History

2023-02-22 12:12:38 +01:00
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
2024-06-18 18:39:02 +02:00
namespace WebAPI;
public class GoogleSheetsHelper
2023-02-22 12:12:38 +01:00
{
2024-06-18 18:39:02 +02:00
public SheetsService? Service { get; private set; }
private const string ApplicationName = "Diuna";
2024-06-18 22:24:04 +02:00
private static readonly string[] Scopes = [SheetsService.Scope.Spreadsheets];
2024-06-18 18:39:02 +02:00
public GoogleSheetsHelper()
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 SheetsService(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
}