-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (75 loc) · 3.15 KB
/
Program.cs
File metadata and controls
84 lines (75 loc) · 3.15 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
using System;
using System.Collections.Generic;
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Extensions.Configuration;
using Helpers;
namespace learn_live_daemon
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var config = LoadAppSettings();
if (config == null)
{
Console.WriteLine("Invalid appsettings.json file.");
return;
}
var client = GetAuthenticatedGraphClient(config);
var requestUserEmail = client.Users[config["targetUserId"]].Messages.Request().Skip(40);
var results = requestUserEmail.GetAsync().Result;
foreach (var message in results)
{
Console.WriteLine("");
Console.WriteLine("Subject : " + message.Subject);
Console.WriteLine("Received: " + message.ReceivedDateTime.ToString());
Console.WriteLine("ID : " + message.Id);
}
Console.WriteLine("\nGraph Request:");
Console.WriteLine(requestUserEmail.GetHttpRequestMessage().RequestUri);
}
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"]) ||
string.IsNullOrEmpty(config["targetUserId"]))
{
return null;
}
return config;
}
catch (System.IO.FileNotFoundException)
{
return null;
}
}
private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config)
{
var authenticationProvider = CreateAuthenticationProvider(config);
return new GraphServiceClient(authenticationProvider);
}
private static IAuthenticationProvider CreateAuthenticationProvider(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();
scopes.Add("https://graph.microsoft.com/.default");
var cca = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithClientSecret(clientSecret)
.Build();
return MsalAuthenticationProvider.GetInstance(cca, scopes.ToArray());
}
}
}