Guest User

Google Calendar API Example

a guest
Oct 28th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 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.IO;
  8. using System.Threading;
  9.  
  10. namespace ConsoleApp1
  11. {
  12.     internal class Program
  13.     {
  14.         private static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
  15.         private static string ApplicationName = "Google Calendar API .NET Quickstart";
  16.  
  17.         private static void Main(string[] args)
  18.         {
  19.             UserCredential credential;
  20.  
  21.             using (var stream =
  22.                 new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
  23.             {
  24.                 // The file token.json stores the user's access and refresh tokens, and is created
  25.                 // automatically when the authorization flow completes for the first time.
  26.                 string credPath = "token.json";
  27.                 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
  28.                     GoogleClientSecrets.Load(stream).Secrets,
  29.                     Scopes,
  30.                     "user",
  31.                     CancellationToken.None,
  32.                     new FileDataStore(credPath, true)).Result;
  33.                 Console.WriteLine("Credential file saved to: " + credPath);
  34.             }
  35.  
  36.             // Create Google Calendar API service.
  37.             var service = new CalendarService(new BaseClientService.Initializer()
  38.             {
  39.                 HttpClientInitializer = credential,
  40.                 ApplicationName = ApplicationName,
  41.             });
  42.  
  43.             // Define parameters of request.
  44.             EventsResource.ListRequest request = service.Events.List("primary");
  45.             request.TimeMin = DateTime.Now;
  46.             request.ShowDeleted = false;
  47.             request.SingleEvents = true;
  48.             request.MaxResults = 10;
  49.             request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
  50.  
  51.             // List events.
  52.             Events events = request.Execute();
  53.             Console.WriteLine("Upcoming events:");
  54.             if (events.Items != null && events.Items.Count > 0)
  55.             {
  56.                 foreach (var eventItem in events.Items)
  57.                 {
  58.                     string when = eventItem.Start.DateTime.ToString();
  59.                     if (String.IsNullOrEmpty(when))
  60.                     {
  61.                         when = eventItem.Start.Date;
  62.                     }
  63.                     Console.WriteLine("{0} ({1})", eventItem.Summary, when);
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine("No upcoming events found.");
  69.             }
  70.             Console.Read();
  71.         }
  72.     }
  73. }
Add Comment
Please, Sign In to add comment