0

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.

1
  • "I am getting an unauthorized error" - but the error message says the error is ResourceNotFound? Commented Jan 15, 2019 at 14:43

1 Answer 1

1

Found the problem, I hadn't set the appSettings correctly.

I added "User.Read Mail.Read.Shared" to my PrivateSettings.Config as shown:

   <add key="ida:AppScopes" value="User.Read Calendars.Read" />
<add key="ida:AppScopes" value="User.Read Mail.Read" />
<add key="ida:AppScopes" value="User.Read Mail.Read.Shared" />

Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.