-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (107 loc) · 3.07 KB
/
Program.cs
File metadata and controls
123 lines (107 loc) · 3.07 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// <ProgramSnippet>
using GraphAppOnlyTutorial;
Console.WriteLine(".NET Graph App-only Tutorial\n");
var settings = Settings.LoadSettings();
// Initialize Graph
InitializeGraph(settings);
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. List users");
Console.WriteLine("3. Make a Graph call");
try
{
choice = int.Parse(Console.ReadLine() ?? string.Empty);
}
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
await DisplayAccessTokenAsync();
break;
case 2:
// List users
await ListUsersAsync();
break;
case 3:
// Run any Graph code
await MakeGraphCallAsync();
break;
default:
Console.WriteLine("Invalid choice! Please try again.");
break;
}
}
// </ProgramSnippet>
// <InitializeGraphSnippet>
void InitializeGraph(Settings settings)
{
GraphHelper.InitializeGraphForAppOnlyAuth(settings);
}
// </InitializeGraphSnippet>
// <DisplayAccessTokenSnippet>
async Task DisplayAccessTokenAsync()
{
try
{
var appOnlyToken = await GraphHelper.GetAppOnlyTokenAsync();
Console.WriteLine($"App-only token: {appOnlyToken}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting app-only access token: {ex.Message}");
}
}
// </DisplayAccessTokenSnippet>
// <ListUsersSnippet>
async Task ListUsersAsync()
{
try
{
var userPage = await GraphHelper.GetUsersAsync();
if (userPage?.Value == null)
{
Console.WriteLine("No results returned.");
return;
}
// Output each users's details
foreach (var user in userPage.Value)
{
Console.WriteLine($"User: {user.DisplayName ?? "NO NAME"}");
Console.WriteLine($" ID: {user.Id}");
Console.WriteLine($" Email: {user.Mail ?? "NO EMAIL"}");
}
// If NextPageRequest is not null, there are more users
// available on the server
// Access the next page like:
// var nextPageRequest = new UsersRequestBuilder(userPage.OdataNextLink, _appClient.RequestAdapter);
// var nextPage = await nextPageRequest.GetAsync();
var moreAvailable = !string.IsNullOrEmpty(userPage.OdataNextLink);
Console.WriteLine($"\nMore users available? {moreAvailable}");
}
catch (Exception ex)
{
Console.WriteLine($"Error getting users: {ex.Message}");
}
}
// </ListUsersSnippet>
// <MakeGraphCallSnippet>
async Task MakeGraphCallAsync()
{
await GraphHelper.MakeGraphCallAsync();
}
// </MakeGraphCallSnippet>