I have an application which can access my mailbox. I created the application following this tutorial:
https://learn.microsoft.com/en-us/graph/tutorials/aspnet?tutorial-step=1
I have then adapted the application to read mail. This works fine for my own mail. However, I need to access a shared inbox which I do have access to and can read emails in my outlook.
I have attempted to do this using the following code:
public static async Task<IEnumerable<Message>> GetMailAsync()
{
var graphClient = GetAuthenticatedClient();
var mail = await graphClient.Users["[email protected]"].MailFolders.Inbox.Messages.Request()
.GetAsync();
return mail;
}
However, I am getting an unauthorized error:
authorization error Here is my authorization code:
private static GraphServiceClient GetAuthenticatedClient()
{
return new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
// Get the signed in user's id and create a token cache
string signedInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
HttpContextWrapper httpContext = new HttpContextWrapper(HttpContext.Current);
TokenCache tokenStore = new SessionTokenStore(signedInUserId,
httpContext).GetMsalCacheInstance();
var idClient = new ConfidentialClientApplication(
appId, redirectUri, new ClientCredential(appSecret),
tokenStore, null);
var accounts = await idClient.GetAccountsAsync();
// By calling this here, the token can be refreshed
// if it's expired right before the Graph call is made
var result = await idClient.AcquireTokenSilentAsync(
graphScopes.Split(' '), accounts.FirstOrDefault());
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
}));
}
I have added permissions within the application Image of app permissions
Can anyone spot what I'm doing wrong here? Some posts sugeest it can't be done this way (Microsoft Graph API .NET not able to read shared mail, Microsoft Graph API SDK .NET Issues getting other users emails), but I can get it working in the graph explorer.
Any help appreciated including advice on how I can improve my questions.
ResourceNotFound?