using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.Sheets.v4; using System; using System.IO; namespace framework_business.handlers { public class GoogleSheetsHelper { public SheetsService Service { get; set; } //const string APPLICATIONNAME = "FastTrack Master Data Sheet"; static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets }; public GoogleSheetsHelper(string applicationName) { InitializeService(applicationName); } private void InitializeService(string applicationName) { var credential = GetCredentialsFromFile(); Service = new SheetsService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = applicationName }); } private GoogleCredential GetCredentialsFromFile() { GoogleCredential credential=null; try { string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GoogleSheetAPISecretes.json"); using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); } return credential; } catch (Exception ex) { return credential; } } } }