Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Google.Apis.Auth.OAuth2;
- using Google.Apis.Calendar.v3;
- using Google.Apis.Calendar.v3.Data;
- using Google.Apis.Services;
- using Google.Apis.Util.Store;
- using System;
- using System.IO;
- using System.Threading;
- namespace ConsoleApp1
- {
- internal class Program
- {
- private static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
- private static string ApplicationName = "Google Calendar API .NET Quickstart";
- private static void Main(string[] args)
- {
- UserCredential credential;
- using (var stream =
- new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
- {
- // The file token.json stores the user's access and refresh tokens, and is created
- // automatically when the authorization flow completes for the first time.
- string credPath = "token.json";
- credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
- GoogleClientSecrets.Load(stream).Secrets,
- Scopes,
- "user",
- CancellationToken.None,
- new FileDataStore(credPath, true)).Result;
- Console.WriteLine("Credential file saved to: " + credPath);
- }
- // Create Google Calendar API service.
- var service = new CalendarService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = ApplicationName,
- });
- // Define parameters of request.
- EventsResource.ListRequest request = service.Events.List("primary");
- request.TimeMin = DateTime.Now;
- request.ShowDeleted = false;
- request.SingleEvents = true;
- request.MaxResults = 10;
- request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
- // List events.
- Events events = request.Execute();
- Console.WriteLine("Upcoming events:");
- if (events.Items != null && events.Items.Count > 0)
- {
- foreach (var eventItem in events.Items)
- {
- string when = eventItem.Start.DateTime.ToString();
- if (String.IsNullOrEmpty(when))
- {
- when = eventItem.Start.Date;
- }
- Console.WriteLine("{0} ({1})", eventItem.Summary, when);
- }
- }
- else
- {
- Console.WriteLine("No upcoming events found.");
- }
- Console.Read();
- }
- }
- }
Add Comment
Please, Sign In to add comment