I create ASP.Net MVC application that use "get access without a user" scenario and I have granted all needed permission as application permission(Calendars.Read) using admin consent. I can already get token and can use that token to get user profile successfully. But, when I try to get calendar events data it get error code "AuthenticationError" and error message "Error authenticating with resource". I use C# code like this:
var events = await graphClient.Users[principalName].Events
.Request()
.GetAsync();
Can Anyone suggest What things I missed or What permission I should use?
My code to get authentication is like this, finally I added "WithExtraQueryParameters" to ensure that I use grant_type is client_credentials
private static GraphServiceClient GetAuthenticated()
{
AuthenticationResult result = null;
var query = new Dictionary<string, string>();
query.Add("grant_type", "client_credentials");
var idClient = ConfidentialClientApplicationBuilder
.Create(appId)
.WithAuthority(AzureCloudInstance.AzurePublic, $"{tenantId}")
.WithClientSecret(appSecret)
.WithExtraQueryParameters(query)
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(idClient);
return new GraphServiceClient(new DelegateAuthenticationProvider(
async (requestMessage) => {
var accounts = await idClient.GetAccountsAsync();
if (accounts.Any())
{
result = await idClient.AcquireTokenSilent(graphScopes?.Split(','), accounts.FirstOrDefault()).ExecuteAsync();
}
else {
result = await idClient.AcquireTokenForClient(scopes).ExecuteAsync();
}
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
}
));
}