Files
DiunaBI/WebAPI/GoogleSheetsHelper.cs
Michał Zieliński e174a02b54 Cleanup .net warnings
2023-11-29 22:14:37 +01:00

40 lines
1.2 KiB
C#

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
namespace WebAPI
{
public class GoogleSheetsHelper
{
public SheetsService? Service { get; set; }
const string APPLICATION_NAME = "Diuna";
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 = APPLICATION_NAME
});
}
private GoogleCredential GetCredentialsFromFile()
{
string fileName = "client_secrets.json";
#if DEBUG
fileName = "client_secrets.Development.json";
#endif
GoogleCredential credential;
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential;
}
}
}