I want to create a service that consumes Microsoft Graph API with reading the Outlook Calendar with this code
public class AuthenticationHelper
{
private static string _clientId = "xxx";
private static string _tenantId = "xxx";
private static string _clientSecret = "xxx";
private static string[] _scopes = new string[] { "https://graph.microsoft.com/.default" };
public static async Task<string> GetAccessTokenAsync()
{
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(_clientId)
.WithClientSecret(_clientSecret)
.WithAuthority(new Uri($"https://login.microsoftonline.com/{_tenantId}"))
.Build();
AuthenticationResult result = await app.AcquireTokenForClient(_scopes).ExecuteAsync();
return result.AccessToken;
}
}
//Call Microsoft Graph API
public class CalendarHelper
{
private static GraphServiceClient GetGraphClient(string accessToken)
{
return new GraphServiceClient(
new DelegateAuthenticationProvider((requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return Task.CompletedTask;
}));
}
public static async Task ListCalendarEventsAsync()
{
string accessToken = await AuthenticationHelper.GetAccessTokenAsync();
var graphClient = GetGraphClient(accessToken);
var events = await graphClient.Me.Calendar.GetAsync();
Console.WriteLine(events);
}
}type here
At the CalendarHelper class, the GraphServiceClient is unable to find the DelegateAuthenticationProvider of the GraphServiceClient method.
Does anyone can solve my code this pattern?
New contributor
Pongsakorn Pakkhemayang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.