簡體   English   中英

如何在 .net core 中使用 Microsoft Graph 訪問共享郵箱

[英]How to access shared mailbox using Microsoft Graph in .net core

我有一個可以訪問我郵箱的應用程序。 我按照本教程創建了應用程序:

https://docs.microsoft.com/en-us/graph/tutorials/aspnet?tutorial-step=1

然后我調整了應用程序來閱讀郵件。 這適用於我自己的郵件。 但是,我需要訪問共享收件箱,我可以訪問該收件箱並且可以在 Outlook 中閱讀電子郵件。

我嘗試使用以下代碼執行此操作:

public static async Task<IEnumerable<Message>> GetMailAsync()
        {
            var graphClient = GetAuthenticatedClient();


            var mail = await graphClient.Users["usersemail@somewhere.com"].MailFolders.Inbox.Messages.Request()
                .GetAsync();

            return mail;
        }

但是,我收到一個未經授權的錯誤:

授權錯誤這是我的授權碼:

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);
                    }));
        }

我在應用程序中添加了權限 應用程序權限圖片

誰能發現我在這里做錯了什么? 有些帖子認為它不能通過這種方式完成( Microsoft Graph API .NET 無法讀取共享郵件Microsoft Graph API SDK .NET 問題獲取其他用戶的電子郵件),但我可以在圖形資源管理器中使用它。

感謝任何幫助,包括有關如何改進我的問題的建議。

發現問題,我沒有正確設置appSettings。

我將“User.Read Mail.Read.Shared”添加到我的 PrivateSettings.Config 中,如下所示:

   <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" />

希望這可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM