forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthHelpers.cs
49 lines (43 loc) · 1.79 KB
/
OAuthHelpers.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Graph;
namespace Microsoft.BotBuilderSamples
{
// This class calls the Microsoft Graph API. The following OAuth scopes are used:
// 'openid' 'profile' 'User.Read'
// for more information about scopes see:
// https://developer.microsoft.com/en-us/graph/docs/concepts/permissions_reference
public static class OAuthHelpers
{
// Send the user their Graph Display Name from the bot.
public static async Task ListMeAsync(ITurnContext turnContext, TokenResponse tokenResponse)
{
var user = await GetUserAsync(turnContext, tokenResponse);
await turnContext.SendActivityAsync($"You are {user.DisplayName}.");
}
// Send the user their Graph Email Address from the bot.
public static async Task ListEmailAddressAsync(ITurnContext turnContext, TokenResponse tokenResponse)
{
var user = await GetUserAsync(turnContext, tokenResponse);
await turnContext.SendActivityAsync($"Your email: {user.Mail}.");
}
private static async Task<User> GetUserAsync(ITurnContext turnContext, TokenResponse tokenResponse)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (tokenResponse == null)
{
throw new ArgumentNullException(nameof(tokenResponse));
}
// Pull in the data from the Microsoft Graph.
var client = new SimpleGraphClient(tokenResponse.Token);
return await client.GetMeAsync();
}
}
}