wingman007

GoogleApiDotNetConsoleCalendarQuickstart

Nov 30th, 2015
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using Google.Apis.Auth.OAuth2;
  2. using Google.Apis.Calendar.v3;
  3. using Google.Apis.Calendar.v3.Data;
  4. using Google.Apis.Services;
  5. using Google.Apis.Util.Store;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13.  
  14. namespace CalendarQuickstart
  15. {
  16.     class Program
  17.     {
  18. // We have followed this tutorial: https://developers.google.com/google-apps/calendar/quickstart/dotnet
  19. // you need to run Install-Package Google.Apis.Calendar.v3
  20. // also you need to create an app in Google Developers Console, download the JSON file and put in the app.
  21.         static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
  22.         static string ApplicationName = "Google Calendar API .NET Quickstart";
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             UserCredential credential;
  27.  
  28.             using (var stream =
  29.                 new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
  30.             {
  31.                 string credPath = System.Environment.GetFolderPath(
  32.                     System.Environment.SpecialFolder.Personal);
  33.                 credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart");
  34.  
  35.                 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  36.                     GoogleClientSecrets.Load(stream).Secrets,
  37.                     Scopes,
  38.                     "user",
  39.                     CancellationToken.None,
  40.                     new FileDataStore(credPath, true)).Result;
  41.                 Console.WriteLine("Credential file saved to: " + credPath);
  42.             }
  43.  
  44.             // Create Google Calendar API service.
  45.             var service = new CalendarService(new BaseClientService.Initializer()
  46.             {
  47.                 HttpClientInitializer = credential,
  48.                 ApplicationName = ApplicationName,
  49.             });
  50.  
  51.             // Define parameters of request.
  52.             EventsResource.ListRequest request = service.Events.List("primary");
  53.             request.TimeMin = DateTime.Now;
  54.             request.ShowDeleted = false;
  55.             request.SingleEvents = true;
  56.             request.MaxResults = 10;
  57.             request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
  58.  
  59.             // List events.
  60.             Events events = request.Execute();
  61.             Console.WriteLine("Upcoming events:");
  62.             if (events.Items != null && events.Items.Count > 0)
  63.             {
  64.                 foreach (var eventItem in events.Items)
  65.                 {
  66.                     string when = eventItem.Start.DateTime.ToString();
  67.                     if (String.IsNullOrEmpty(when))
  68.                     {
  69.                         when = eventItem.Start.Date;
  70.                     }
  71.                     Console.WriteLine("{0} ({1})", eventItem.Summary, when);
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 Console.WriteLine("No upcoming events found.");
  77.             }
  78.             Console.Read();
  79.  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment