GraphAPI allows me to authenticate in my application, but returns Resource could not be discovered when attempting to fetch my email messages, calendar, or anything else. After authenticating, the scope returned from the auth request matches the scope I've setup in the Azure portal for the app. My profile info is also correct.
I'd greatly appreciate any help with this.
Thanks!
string scope = "https://graph.microsoft.com/.default";
var builder = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority($"{Authority}{Tenant}")
.WithDefaultRedirectUri();
_clientApp = builder.Build();
TokenCacheHelper.EnableSerialization(PublicClientApp.UserTokenCache);
AuthenticationResult authResult = null;
IAccount firstAccount;
var accounts = await PublicClientApp.GetAccountsAsync();
firstAccount = accounts.FirstOrDefault();
try
{
authResult = await PublicClientApp.AcquireTokenSilent(scope, firstAccount)
.ExecuteAsync();
}
catch (MsalUiRequiredException)
{
try
{
authResult = await PublicClientApp.AcquireTokenInteractive(scope)
.WithAccount(firstAccount)
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
.ExecuteAsync();
}
catch (MsalException msalex)
{
Console.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
return false;
}
if (authResult != null)
{
try
{
var graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
return (Task.FromResult(0));
}
));
var me = await graphClient.Me.Request().GetAsync();
mUserName = me.DisplayName; // This works!
try
{
var messages = await graphClient.Me.Messages
.Request() // This fails
.GetAsync();
}
catch (Exception) {}
}
}
}