I am trying to write code to access Microsoft Calendar using Graph API
var calendars = await graphClient.Me.Calendars.Request().GetAsync();
List<string> calendarNames = new List<string>();
foreach (var calendar in calendars)
{
calendarNames.Add(calendar.Name);
}
I am facing strange problem.
var calendars = await graphClient.Me.Calendars.Request().GetAsync();
But compiler is throwing error saying "'CalendarsRequestBuilder' does not contain a definition for 'Request' "
I have nuget packages included
Microsoft.Graph Microsoft.Graph.Core Microsoft.Idendity.Client
I tried downgrading Graph library, but problem still remains.
The Request()
method is used in Graph SDK v4, the latest SDK v5 removed this method, so you can call directly GetAsync()
await graphClient.Me.Calendars.GetAsync();
To get access token (based on your comment)
IAccessTokenProvider
GetAuthorizationTokenAsync
create IPublicClientApplication
public class TokenProvider : IAccessTokenProvider
{
public AllowedHostsValidator AllowedHostsValidator => throw new NotImplementedException();
public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string, object> additionalAuthenticationContext = null, CancellationToken cancellationToken = default)
{
var scopes = new[] { "Calendar.ReadWrite" };
AuthenticationResult authResult;
var publicClientApp = PublicClientApplicationBuilder.Create("clientId").Build();
try
{
var accounts = await publicClientApp.GetAccountsAsync();
authResult = await publicClientApp.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
return authResult.AccessToken;
}
catch (MsalUiRequiredException ex)
{
authResult = await publicClientApp.AcquireTokenInteractive(scopes).ExecuteAsync();
return authResult.AccessToken;
}
}
}
Now, create a new authentication provider
var authenticationProvider = new BaseBearerTokenAuthenticationProvider(new TokenProvider());
var graphClient = new GraphServiceClient(authenticationProvider);