【问题标题】:Microsoft Graph API unable to Send Email C# ConsoleMicrosoft Graph API 无法发送电子邮件 C# 控制台
【发布时间】:2019-08-09 16:18:31
【问题描述】:

我创建了一个小型 Console App 以使用 Microsoft Graph API 发送电子邮件。

教程使用

https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=csharp

错误

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;

    }

}

}

这是我授予 AD APP 的权限截图

那么,谁能指导我哪里出错了

【问题讨论】:

  • 您是否明确授予权限? “单击授予权限按钮以实际授予应用程序权限。管理员必须授予这些权限,您可以通过单击按钮来执行此操作。您在为应用程序启用的权限中所做的任何更改都需要您明确授予通过单击按钮获得权限。”
  • 不,这对我来说是禁用的,我不明白,为什么我不能使用委派的,以我自己的用户身份发送邮件。为什么我需要使用应用级权限。
  • 因为您使用的是 ClientCredentialsProvider,它不允许用户委托流程。

标签: c# azure azure-active-directory azure-ad-graph-api microsoft-graph-mail


【解决方案1】:

根据您的屏幕截图,您尚未授予管理员对Mail.Send 应用程序权限的同意。

点击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);

【讨论】:

  • 谢谢托尼,那个按钮对我来说是禁用的,获得应用程序级别的权限并不容易。我不能简单地使用委托的权限吗?因为根据微软提供的文档,我只需要其中一个权限。
  • @TusharNarang 是的,你可以。但是您获得访问令牌的方式是需要应用程序许可的客户端凭据流。要使用委托权限,您需要通过用户身份验证来访问图形。参考docs.microsoft.com/en-us/graph/sdks/…
  • 我使用了交互式提供程序并收到以下错误 ServiceException:代码:ErrorAccessDenied 消息:对 OData 的访问被禁用。内部错误
  • @TusharNarang 检查更新的答案,你的代码和我的一样吗?
  • 嗨托尼,我的是一个控制台,所以我不明白为什么我需要将它重定向到本地主机。但是你确实尝试了交互式但我得到了一个例外有没有办法通过管理员同意发送邮件权限进行身份验证。
猜你喜欢
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多