-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
87 lines (75 loc) · 3.01 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Helpers;
using Microsoft.Extensions.Configuration;
using Microsoft.Graph;
using Microsoft.Identity.Client;
namespace graphdaemon
{
class Program
{
static async Task Main(string[] args)
{
var config = LoadAppSettings();
if (config == null)
{
Console.WriteLine("Invalid appsettings.json file.");
return;
}
var client = GetAuthenticatedGraphClient(config);
var users = await client.Users.Request().Select("id").GetAsync();
foreach (var user in users)
{
var queryOptions = new List<QueryOption>()
{
new QueryOption("startdatetime", "2021-05-03T15:49:18.531Z"),
new QueryOption("enddatetime", "2021-05-10T15:49:18.531Z")
};
var calendarView = await client.Users[user.Id].CalendarView
.Request(queryOptions)
.GetAsync();
}
}
private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config)
{
var authenticationProvider = CreateAuthorizationProvider(config);
return new GraphServiceClient(authenticationProvider);
}
private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config)
{
var tenantId = config["tenantId"];
var clientId = config["applicationId"];
var clientSecret = config["applicationSecret"];
var authority = $"https://login.microsoftonline.com/{config["tenantId"]}/v2.0";
List<string> scopes = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
var cca = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithClientSecret(clientSecret)
.Build();
return MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray());
}
private static IConfigurationRoot LoadAppSettings()
{
try
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.Build();
if (string.IsNullOrEmpty(config["applicationId"]) ||
string.IsNullOrEmpty(config["applicationSecret"]) ||
string.IsNullOrEmpty(config["tenantId"]))
{
return null;
}
return config;
}
catch (System.IO.FileNotFoundException)
{
return null;
}
}
}
}