【发布时间】: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 mail,Microsoft Graph API SDK .NET Issues getting other users emails),但我可以让它在图形资源管理器中工作。
感谢任何帮助,包括关于如何改进我的问题的建议。
【问题讨论】:
-
“我收到未经授权的错误” - 但错误消息显示错误为
ResourceNotFound?
标签: c# .net outlook microsoft-graph-api