- Notifications
You must be signed in to change notification settings - Fork 843
/
Copy pathSimpleGraphClient.cs
175 lines (153 loc) · 6.19 KB
/
SimpleGraphClient.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Net.Http.Headers;
usingSystem.Threading.Tasks;
usingMicrosoft.Graph;
namespaceMicrosoft.BotBuilderSamples
{
/// <summary>
/// This class is a wrapper for the Microsoft Graph API.
/// See: https://developer.microsoft.com/en-us/graph
/// </summary>
publicclassSimpleGraphClient
{
privatereadonlystring_token;
/// <summary>
/// Initializes a new instance of the <see cref="SimpleGraphClient"/> class.
/// </summary>
/// <param name="token">The OAuth token.</param>
publicSimpleGraphClient(stringtoken)
{
if(string.IsNullOrWhiteSpace(token))
{
thrownewArgumentNullException(nameof(token));
}
_token=token;
}
/// <summary>
/// Sends an email on the user's behalf using the Microsoft Graph API.
/// </summary>
/// <param name="toAddress">The recipient's email address.</param>
/// <param name="subject">The email subject.</param>
/// <param name="content">The email content.</param>
/// <returns>A task that represents the work queued to execute.</returns>
publicasyncTaskSendMailAsync(stringtoAddress,stringsubject,stringcontent)
{
if(string.IsNullOrWhiteSpace(toAddress))
{
thrownewArgumentNullException(nameof(toAddress));
}
if(string.IsNullOrWhiteSpace(subject))
{
thrownewArgumentNullException(nameof(subject));
}
if(string.IsNullOrWhiteSpace(content))
{
thrownewArgumentNullException(nameof(content));
}
vargraphClient=GetAuthenticatedClient();
varrecipients=newList<Recipient>
{
newRecipient
{
EmailAddress=newEmailAddress
{
Address=toAddress,
},
},
};
// Create the message.
varemail=newMessage
{
Body=newItemBody
{
Content=content,
ContentType=BodyType.Text,
},
Subject=subject,
ToRecipients=recipients,
};
// Send the message.
awaitgraphClient.Me.SendMail(email,true).Request().PostAsync();
}
/// <summary>
/// Gets recent mail for the user using the Microsoft Graph API.
/// </summary>
/// <returns>An array of recent messages.</returns>
publicasyncTask<Message[]>GetRecentMailAsync()
{
vargraphClient=GetAuthenticatedClient();
varmessages=awaitgraphClient.Me.MailFolders.Inbox.Messages.Request().GetAsync();
returnmessages.Take(5).ToArray();
}
/// <summary>
/// Gets information about the user.
/// </summary>
/// <returns>The user information.</returns>
publicasyncTask<User>GetMeAsync()
{
vargraphClient=GetAuthenticatedClient();
varme=awaitgraphClient.Me.Request().GetAsync();
returnme;
}
/// <summary>
/// Gets information about the user's manager.
/// </summary>
/// <returns>The manager information.</returns>
publicasyncTask<User>GetManagerAsync()
{
vargraphClient=GetAuthenticatedClient();
varmanager=awaitgraphClient.Me.Manager.Request().GetAsync()asUser;
returnmanager;
}
// // Gets the user's photo
// public async Task<PhotoResponse> GetPhotoAsync()
// {
// HttpClient client = new HttpClient();
// client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token);
// client.DefaultRequestHeaders.Add("Accept", "application/json");
// using (var response = await client.GetAsync("https://graph.microsoft.com/v1.0/me/photo/$value"))
// {
// if (!response.IsSuccessStatusCode)
// {
// throw new HttpRequestException($"Graph returned an invalid success code: {response.StatusCode}");
// }
// var stream = await response.Content.ReadAsStreamAsync();
// var bytes = new byte[stream.Length];
// stream.Read(bytes, 0, (int)stream.Length);
// var photoResponse = new PhotoResponse
// {
// Bytes = bytes,
// ContentType = response.Content.Headers.ContentType?.ToString(),
// };
// if (photoResponse != null)
// {
// photoResponse.Base64String = $"data:{photoResponse.ContentType};base64," +
// Convert.ToBase64String(photoResponse.Bytes);
// }
// return photoResponse;
// }
// }
/// <summary>
/// Gets an authenticated Microsoft Graph client using the token issued to the user.
/// </summary>
/// <returns>The authenticated GraphServiceClient.</returns>
privateGraphServiceClientGetAuthenticatedClient()
{
vargraphClient=newGraphServiceClient(
newDelegateAuthenticationProvider(
requestMessage =>
{
// Append the access token to the request.
requestMessage.Headers.Authorization=newAuthenticationHeaderValue("bearer",_token);
// Get event times in the current time zone.
requestMessage.Headers.Add("Prefer","outlook.timezone=\""+TimeZoneInfo.Local.Id+"\"");
returnTask.CompletedTask;
}));
returngraphClient;
}
}
}