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.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace CalendarQuickstart
- {
- class Program
- {
- // We have followed this tutorial: https://developers.google.com/google-apps/calendar/quickstart/dotnet
- // you need to run Install-Package Google.Apis.Calendar.v3
- // also you need to create an app in Google Developers Console, download the JSON file and put in the app.
- static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
- static string ApplicationName = "Google Calendar API .NET Quickstart";
- static void Main(string[] args)
- {
- UserCredential credential;
- using (var stream =
- new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
- {
- string credPath = System.Environment.GetFolderPath(
- System.Environment.SpecialFolder.Personal);
- credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart");
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment