-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
129 lines (107 loc) · 4.25 KB
/
Program.cs
File metadata and controls
129 lines (107 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
namespace GraphTutorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(".NET Core Graph Tutorial\n");
var appConfig = LoadAppSettings();
if (appConfig == null)
{
Console.WriteLine("Missing or invalid appsettings.json...exiting");
return;
}
var appId = appConfig["appId"];
var scopesString = appConfig["scopes"];
var scopes = scopesString.Split(';');
// Initialize the auth provider with values from appsettings.json
var authProvider = new DeviceCodeAuthProvider(appId, scopes);
// Request a token to sign in the user
var accessToken = authProvider.GetAccessToken().Result;
// Initialize Graph client
GraphHelper.Initialize(authProvider);
// Get signed in user
var user = GraphHelper.GetMeAsync().Result;
Console.WriteLine($"Welcome {user.DisplayName}!\n");
int choice = -1;
while (choice != 0) {
Console.WriteLine("Please choose one of the following options:");
Console.WriteLine("0. Exit");
Console.WriteLine("1. Display access token");
Console.WriteLine("2. View this week's calendar");
Console.WriteLine("3. Add an event");
try
{
choice = int.Parse(Console.ReadLine());
}
catch (System.FormatException)
{
// Set to invalid value
choice = -1;
}
switch(choice)
{
case 0:
// Exit the program
Console.WriteLine("Goodbye...");
break;
case 1:
// Display access token
Console.WriteLine($"Access token: {accessToken}\n");
break;
case 2:
// List the calendar
ListCalendarEvents(
user.MailboxSettings.TimeZone,
$"{user.MailboxSettings.DateFormat} {user.MailboxSettings.TimeFormat}"
);
break;
case 3:
// Create a new event
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
break;
}
}
}
static IConfigurationRoot LoadAppSettings()
{
var appConfig = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.Build();
// Check for required settings
if (string.IsNullOrEmpty(appConfig["appId"]) ||
string.IsNullOrEmpty(appConfig["scopes"]))
{
return null;
}
return appConfig;
}
static string FormatDateTimeTimeZone(
Microsoft.Graph.DateTimeTimeZone value,
string dateTimeFormat)
{
// Parse the date/time string from Graph into a DateTime
var dateTime = DateTime.Parse(value.DateTime);
return dateTime.ToString(dateTimeFormat);
}
static void ListCalendarEvents(string userTimeZone, string dateTimeFormat)
{
var events = GraphHelper
.GetCurrentWeekCalendarViewAsync(DateTime.Today, userTimeZone)
.Result;
Console.WriteLine("Events:");
foreach (var calendarEvent in events)
{
Console.WriteLine($"Subject: {calendarEvent.Subject}");
Console.WriteLine($" Organizer: {calendarEvent.Organizer.EmailAddress.Name}");
Console.WriteLine($" Start: {FormatDateTimeTimeZone(calendarEvent.Start, dateTimeFormat)}");
Console.WriteLine($" End: {FormatDateTimeTimeZone(calendarEvent.End, dateTimeFormat)}");
}
}
}
}