-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleGraphClient.cs
152 lines (130 loc) · 5.13 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Graph;
namespace Microsoft.BotBuilderSamples
{
// This class is a wrapper for the Microsoft Graph API
// See: https://developer.microsoft.com/en-us/graph
public class SimpleGraphClient
{
private readonly string _token;
public SimpleGraphClient(string token)
{
if (string.IsNullOrWhiteSpace(token))
{
throw new ArgumentNullException(nameof(token));
}
_token = token;
}
// Sends an email on the users behalf using the Microsoft Graph API
public async Task SendMailAsync(string toAddress, string subject, string content)
{
if (string.IsNullOrWhiteSpace(toAddress))
{
throw new ArgumentNullException(nameof(toAddress));
}
if (string.IsNullOrWhiteSpace(subject))
{
throw new ArgumentNullException(nameof(subject));
}
if (string.IsNullOrWhiteSpace(content))
{
throw new ArgumentNullException(nameof(content));
}
var graphClient = GetAuthenticatedClient();
var recipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = toAddress,
},
},
};
// Create the message.
var email = new Message
{
Body = new ItemBody
{
Content = content,
ContentType = BodyType.Text,
},
Subject = subject,
ToRecipients = recipients,
};
// Send the message.
await graphClient.Me.SendMail(email, true).Request().PostAsync();
}
// Gets mail for the user using the Microsoft Graph API
public async Task<Message[]> GetRecentMailAsync()
{
var graphClient = GetAuthenticatedClient();
var messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().GetAsync();
return messages.Take(5).ToArray();
}
// Get information about the user.
public async Task<User> GetMeAsync()
{
var graphClient = GetAuthenticatedClient();
var me = await graphClient.Me.Request().GetAsync();
return me;
}
// gets information about the user's manager.
public async Task<User> GetManagerAsync()
{
var graphClient = GetAuthenticatedClient();
var manager = await graphClient.Me.Manager.Request().GetAsync() as User;
return manager;
}
// 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;
}
}
// Get an Authenticated Microsoft Graph client using the token issued to the user.
private GraphServiceClient GetAuthenticatedClient()
{
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
requestMessage =>
{
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _token);
// Get event times in the current time zone.
requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");
return Task.CompletedTask;
}));
return graphClient;
}
}
}