我有一个可以访问我的邮箱的应用程序。我按照本教程创建了该应用程序:
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 mail,Microsoft Graph API SDK .NET Issues getting other users emails),但我可以在图形浏览器中使用它。
任何帮助表示赞赏,包括有关如何改善问题的建议。
答案 0 :(得分:0)
发现问题,我没有正确设置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" />
希望这对某人有帮助。