【问题标题】:How to access shared mailbox using Microsoft Graph in .net core如何在 .net core 中使用 Microsoft Graph 访问共享邮箱
【发布时间】:2019-01-15 14:40:34
【问题描述】:

我有一个可以访问我邮箱的应用程序。我按照本教程创建了应用程序:

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

但是,我收到一个未经授权的错误:

authorization error 这是我的授权码:

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

我在应用程序Image of app permissions中添加了权限

谁能发现我在这里做错了什么?有些帖子认为它不能以这种方式完成(Microsoft Graph API .NET not able to read shared mailMicrosoft Graph API SDK .NET Issues getting other users emails),但我可以让它在图形资源管理器中工作。

感谢任何帮助,包括关于如何改进我的问题的建议。

【问题讨论】:

  • “我收到未经授权的错误” - 但错误消息显示错误为 ResourceNotFound?

标签: c# .net outlook microsoft-graph-api


【解决方案1】:

发现问题,我没有正确设置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" />

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    相关资源
    最近更新 更多