【发布时间】: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