Microsoft Graph API无法通过C#控制台发送电子邮件

Microsoft Graph API无法通过C#控制台发送电子邮件,c#,azure,azure-active-directory,azure-ad-graph-api,microsoft-graph-mail,C#,Azure,Azure Active Directory,Azure Ad Graph Api,Microsoft Graph Mail,我已经创建了一个小的控制台应用程序,使用MicrosoftGraphAPI发送电子邮件 使用的教程 错误 ServiceException:代码:NoPermissionsInAccessToken消息:令牌 不包含权限,或权限无法理解 代码 using System; using System.Collections.Generic; using System.Linq; using System.IO; using Microsoft.Graph; using Microsoft.Grap

我已经创建了一个小的控制台应用程序,使用MicrosoftGraphAPI发送电子邮件

使用的教程

错误

ServiceException:代码:NoPermissionsInAccessToken消息:令牌 不包含权限,或权限无法理解

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Graph.Extensions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;

namespace GraphAPI
 {
    class Program
    {

    static void Main(string[] args)
    {
        // Azure AD APP
        string clientId = "<client Key Here>";
        string tenantID = "<tenant key here>";
        string clientSecret = "<client secret here>";

        Task<GraphServiceClient> callTask = Task.Run(() => SendEmail(clientId, tenantID, clientSecret));
        // Wait for it to finish
        callTask.Wait();
        // Get the result
        var astr = callTask;
    }

    public static async Task<GraphServiceClient> SendEmail(string clientId, string tenantID, string clientSecret)
    {

        IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithTenantId(tenantID)
            .WithClientSecret(clientSecret)
            .Build();

        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);       

        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        var message = new Message
        {
            Subject = "Meet for lunch?",
            Body = new ItemBody
            {
                ContentType = BodyType.Text,
                Content = "The new cafeteria is open."
            },
            ToRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "myToEmail@gmail.com"
                    }
                }
            },
            CcRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "myCCEmail@gmail.com"
                    }
                }
            }
        };

        var saveToSentItems = true;

          await graphClient.Me
            .SendMail(message, saveToSentItems)
            .Request()
            .PostAsync();

        return graphClient;

    }

}

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用Microsoft.Graph;
使用Microsoft.Graph.Auth;
使用Microsoft.Graph.Extensions;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.Identity.Client;
名称空间图形
{
班级计划
{
静态void Main(字符串[]参数)
{
//Azure广告应用程序
字符串clientId=“”;
字符串tenantID=“”;
字符串clientSecret=“”;
Task callTask=Task.Run(()=>sendmail(clientId、tenantID、clientSecret));
//等它结束
callTask.Wait();
//得到结果
var asr=callTask;
}
公共静态异步任务SendEmail(字符串clientId、字符串tenantID、字符串clientSecret)
{
IConfidentialClientApplication-secretentialclientapplication=secretentialclientapplicationbuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider=新的ClientCredentialProvider(机密客户端应用程序);
GraphServiceClient graphClient=新的GraphServiceClient(authProvider);
var消息=新消息
{
主题=“见面吃午饭?”,
Body=新项目Body
{
ContentType=BodyType.Text,
Content=“新自助餐厅已开放。”
},
ToRecipients=新列表()
{
新收件人
{
EmailAddress=新的EmailAddress
{
地址=”myToEmail@gmail.com"
}
}
},
CcRecipients=新列表()
{
新收件人
{
EmailAddress=新的EmailAddress
{
地址=”myCCEmail@gmail.com"
}
}
}
};
var saveToSentItems=true;
等我来
.SendMail(消息,保存到存储项)
.Request()
.PostAsync();
返回图形客户端;
}
}
}
这是我给这个广告应用程序的权限的屏幕截图


那么,有谁能告诉我哪里出了问题

根据你的截图,你还没有授予管理员对
邮件发送的许可

单击api权限下的授予管理员同意按钮

更新:

交互式提供者:

            string[] scopes = { "Mail.Send" };
            string clientId = "";
            IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
            .Create(clientId)
            .WithRedirectUri("https://localhost")
            .Build();

            InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);

            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

您是否明确授予了权限?单击“授予权限”按钮可实际授予应用程序权限。管理员必须授予这些权限,您可以通过单击按钮来授予。您对应用程序启用的权限所做的任何更改都要求您通过单击按钮显式授予权限。“否,这对我是禁用的,我不明白,为什么我不能使用委托人,以我自己的用户身份发送邮件。为什么我需要使用应用程序级权限。谢谢Tony,那个按钮对我来说是禁用的,获得应用程序级权限并不容易。我不能简单地使用授权权限。因为根据Microsoft提供的文档,我只需要一个权限。@TusharNarang是的,您可以。但获取访问令牌的方式是需要应用程序权限的客户端凭据流。要使用委派权限,您需要通过用户身份验证访问图形。请参阅,我使用了交互式提供程序并收到以下错误ServiceException:代码:ErrorAccessDenied消息:对OData的访问被禁用。内部的error@TusharNarang检查更新的答案,你的代码和我的一样吗?嗨,托尼,我的是一个控制台,所以我不明白为什么我需要将它重定向到localhost。然而,你确实尝试了interactive,但我得到了一个例外,那就是有一种方法可以通过管理员同意sen邮件权限进行身份验证。