Files
DiunaBI/WebAPI/GoogleSheetsHelper.cs

40 lines
1.2 KiB
C#
Raw Normal View History

2022-12-21 22:15:17 +01:00
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()
{
2023-01-08 18:19:49 +01:00
string fileName = "client_secrets.json";
#if DEBUG
fileName = "client_secrets.Development.json";
#endif
2022-12-21 22:15:17 +01:00
GoogleCredential credential;
2023-01-08 18:19:49 +01:00
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
2022-12-21 22:15:17 +01:00
{
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
}
return credential;
}
}
}