gpt4 book ai didi

azure-active-directory - 无法通过具有委派权限的 Microsoft Graph API 发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 08:40:15 34 4
gpt4 key购买 nike

我创建了一个 C# 控制台应用程序来使用 Microsoft Graph API 发送电子邮件。添加邮件时。发送 申请 许可,它工作正常。但是,由于公司要求,我被要求使用 Mail.Send 委托(delegate)取而代之的是权限,并且有了该权限,我看不到它起作用,并且看到此错误:
enter image description here
添加 Mail.Send 后我应该考虑执行哪些步骤?委托(delegate)允许这个工作?
enter image description here
这是我的代码:

    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)
{

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

var authProvider = new ClientCredentialProvider(confidentialClientApplication);

var graphClient = new GraphServiceClient(authProvider);

var message = new Message
{
Subject = subject,
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = content
},
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress { Address = recipientAddress }
}
}
};

var saveToSentItems = true;

await _graphClient.Users[<userprincipalname>]
.SendMail(message, saveToSentItems)
.Request()
.PostAsync();

return graphClient;

}
更新:
根据以下答案,我将代码更新如下:
var publicClientApplication = PublicClientApplicationBuilder
.Create("<client-id>")
.WithTenantId("<tenant-id>")
.Build();

var authProvider = new UsernamePasswordProvider(publicClientApplication);

var secureString = new NetworkCredential("", "<password>").SecurePassword;

User me = await graphClient.Me.Request()
.WithUsernamePassword("<username>", secureString)
.GetAsync();
我启用了“允许公共(public)客户端流”来修复异常。
enter image description here
现在我看到另一个异常:权限不足,无法完成操作。
我错过了什么?
更新:目前我看到这个异常,代码没有变化:
enter image description here

最佳答案

您提供的代码显示您使用 client credential flow进行身份验证。当您使用 Mail.Send应用程序权限,使用客户端凭据流是可以的。但是如果你使用 Mail.Send委托(delegate)权限,我们不能使用客户凭证。您应该使用 username/password flow进行身份验证。
==================================更新 ====================================
下面是我的代码:

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Security;

namespace ConsoleApp34
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");

var publicClientApplication = PublicClientApplicationBuilder
.Create("client id")
.WithTenantId("tenant id")
.Build();

string[] scopes = new string[] { "mail.send" };

UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);

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 = "to email address"
}
}
}
};

var securePassword = new SecureString();
foreach (char c in "your password")
securePassword.AppendChar(c);

var saveToSentItems = true;

await graphClient.Me
.SendMail(message, saveToSentItems)
.Request().WithUsernamePassword("your email", securePassword)
.PostAsync();
}
}
}
您的错误消息的原因 Insufficient privileges to complete the operation您是否使用以下代码:
User me = await graphClient.Me.Request()
.WithUsernamePassword("<username>", secureString)
.GetAsync();
此代码用于获取用户(我)的信息但不发送电子邮件,您尚未将权限添加到应用程序。所以它会显示 Insufficient privileges to complete the operation .请删除此代码并改用我的代码中的代码块:
await graphClient.Me.SendMail(message, saveToSentItems)
.Request().WithUsernamePassword("your email", securePassword)
.PostAsync();
=============================== 更新2 =====================================
enter image description here
enter image description here

关于azure-active-directory - 无法通过具有委派权限的 Microsoft Graph API 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64602154/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com