使用 Microsoft Graph 访问电子邮件的最简单方法是什么?

问题描述 投票:0回答:1

我是 Microsoft Graph 的新手,我想用它来阅读和移动电子邮件。这是我的 C# 代码:

static async Task Main(string[] args)
{
    var scopes = new[] { "https://graph.microsoft.com/.default" }
    var tenantId = args[0];
    var clientId = args[1];
    var clientSecret = args[2];
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    }
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options)
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes)
    var messsages = await graphClient.Me.Messages.GetAsync();
    Console.WriteLine(messsages.Value.Count);
    Console.WriteLine("Hello, World!");
}

这是我收到的错误消息:

C:\Users\daan\source onrepos\TryMicrosoftGraph\TryOther\Program.cs(26,31):警告 CS8602:取消引用可能的 空引用。 [C:\Users\daan\源 onrepos\TryMicrosoftGraph\TryOther\TryOther.csproj] C:\用户\daan\源 onrepos\TryMicrosoftGraph\TryOther\Program.cs(26,31):警告 CS8602:取消引用可能的 空引用。 [C:\Users\daan\源 onrepos\TryMicrosoftGraph\TryOther\TryOther.csproj] 未处理的异常。 Microsoft.Graph.Models.ODataErrors.ODataError:/me 请求仅在委托身份验证流程中有效。

所以消息是:

/me 请求仅在委托身份验证流程中有效。

我不知道现在该怎么办。我真的需要“委托身份验证流程”吗?或者有更简单的方法来访问我的消息吗?

我之前在 stackoverflow 上见过这样的错误,但是 这样的帖子 没有解释我如何解决它,让我可以访问电子邮件。

.net azure microsoft-graph-api microsoft-graph-sdks microsoft-graph-mail
1个回答
0
投票

您的应用程序是机密应用程序,它通过客户端凭据流请求令牌。这就是错误通知中提到的 /me 路径无法正常工作的原因。

本质上,您仍然可以访问来自组织/租户内任何用户的消息,但您需要构建不同的 URL 来检索这些消息。

例如,您可以使用此 URL 检索用户的消息:

# get a message of a user    
GET /users/{id | userPrincipalName}/messages/{id}

另一个可能对您有帮助的有趣请求:

# list all messages of a user
GET /users/{id | userPrincipalName}/messages

# list all users
GET /users
© www.soinside.com 2019 - 2024. All rights reserved.