diff --git a/.gitignore b/.gitignore index 48cc008..0917f87 100644 --- a/.gitignore +++ b/.gitignore @@ -452,3 +452,4 @@ !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json +/.vscode diff --git a/VQCalendarAttention/CalendarApiTest.cs b/VQCalendarAttention/CalendarApiTest.cs new file mode 100644 index 0000000..7784f1f --- /dev/null +++ b/VQCalendarAttention/CalendarApiTest.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; +using System.Threading; +using Google; +using Google.Apis.Auth.OAuth2; +using Google.Apis.Calendar.v3; +using Google.Apis.Calendar.v3.Data; +using Google.Apis.Services; + +internal class CalendarApiTest +{ + public static async Task GetLatestEventAsync(string calendarId, string credentialsFilePath) + { + try { + + // サービスアカウントの認証情報を読み込む + GoogleCredential credential; + using (var stream = new FileStream(credentialsFilePath, FileMode.Open, FileAccess.Read)) + { + credential = GoogleCredential.FromStream(stream).CreateScoped(CalendarService.Scope.CalendarReadonly); + } + + // GoogleカレンダーAPIのサービスを作成 + var service = new CalendarService(new BaseClientService.Initializer + { + HttpClientInitializer = credential, + ApplicationName = "VQCalendarAttention", + }); + + // イベントリストのリクエストを作成 + var request = service.Events.List(calendarId); + request.TimeMin = DateTime.UtcNow; + request.ShowDeleted = false; + request.SingleEvents = true; + request.MaxResults = 10; + request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime; + + // イベントリストのリクエストを実行 + var events = await request.ExecuteAsync(); + + if (events.Items != null && events.Items.Count > 0) + { + Console.WriteLine("最新の予定:"); + foreach (var eventItem in events.Items) + { + var when = eventItem.Start.DateTime.ToString() ?? eventItem.Start.Date; + Console.WriteLine($"{eventItem.Summary} ({when})"); + } + } + else + { + Console.WriteLine("近日中の予定はありません。"); + } + } + catch (GoogleApiException ex) + { + Console.WriteLine($"Error: {ex.Message}"); + Console.WriteLine($"HttpStatusCode: {ex.HttpStatusCode}"); + Console.WriteLine($"ErrorDetails: {ex.Error}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/VQCalendarAttention/Program.cs b/VQCalendarAttention/Program.cs index c19346f..d9f5c87 100644 --- a/VQCalendarAttention/Program.cs +++ b/VQCalendarAttention/Program.cs @@ -7,9 +7,13 @@ { public class Program { - public static void Main(string[] args) + public static async Task Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + //CreateHostBuilder(args).Build().Run(); + + // テストとしてCalendarApiTestを実行する + await CalendarApiTest.GetLatestEventAsync("3e39821e10e795b311f93c79490960c3196632a465a794ca32e8a44c50115dde@group.calendar.google.com", "../calendartowebhook-fa30e21f081f.json"); + } public static IHostBuilder CreateHostBuilder(string[] args) =>