我是 Microsoft Graph API 的新手。我的要求是读取所有电子邮件附件并将它们插入 SQL 数据库。我的客户只允许委托权限来实现此要求。我遵循下面提到的代码,但它没有向我显示任何错误或任何结果。
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "azure_ad_appid";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var inboxMessages = await graphClient
.Users["[email protected]"]
.MailFolders["inbox"]
.Messages
.Request()
.Expand("attachments")
.Top(20)
.GetAsync();
任何人,请帮我解决这个问题。微软将在本月底禁用基本身份验证,所以我必须在此之前实现这一点。
提前致谢。
我尝试在我的环境中重现相同的结果并得到以下结果:
我运行了与您相同的代码,并得到了相同的结果,没有输出或错误,如下所示:
using Azure.Identity;
using Microsoft.Graph;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "azure_ad_appid";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var inboxMessages = await graphClient
.Users["[email protected]"]
.MailFolders["inbox"]
.Messages
.Request()
.Expand("attachments")
.Top(20)
.GetAsync();
回复:
正如我在评论中提到的,您需要使用像授权码这样的交互流程来获得
Delegated
权限。
我通过使用 Delegated
权限的
Graph Explorer成功获得结果,如下所示:
GET https://graph.microsoft.com/v1.0/me/messages/<messageID>?$expand=attachments
回复:
您可以在此处获取示例 C# 代码片段:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var message = await graphClient.Me.Messages["messageID"]
.Request()
.Expand("attachments")
.GetAsync();
完整示例代码:
using Azure.Identity;
using Microsoft.Graph;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "azure_ad_appid";
var clientSecret = "client_secret";
var authorizationCode = "code that you got in below request";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.authorizationcodecredential
var authCodeCredential = new AuthorizationCodeCredential(
tenantId, clientId, clientSecret, authorizationCode, options);
var graphClient = new GraphServiceClient(authCodeCredential, scopes);
var message = await graphClient.Me.Messages["messageID"]
.Request()
.Expand("attachments")
.GetAsync();
要获取授权
code
值,您可以使用以下请求:
https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
?client_id=<appID>
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345
当我在浏览器中运行上述请求时。我得到了
code
以及 重定向 URI,如下所示:
我以不同的方式实现了这一点。所以我发布我的代码,这样它可能会对某人有所帮助。
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var userNameCredential = new UsernamePasswordCredential(mailboxUser, mailboxPass, tenantId, clientId, options);
var graphClient = new GraphServiceClient(userNameCredential, scopes);
var graphResult = graphClient.Users[mailboxUser].MailFolders["inbox"]
.Messages
.Request()
.Expand("attachments").GetAsync().Result;
var attachmentMessage = graphResult.Where(x => x.HasAttachments == true);
foreach (var message in attachmentMessage)
{
var mailReceiveDate = Convert.ToDateTime(message.ReceivedDateTime.ToString());
string fromEmail = message.From.EmailAddress.Address.ToString();
string mailSubject = message.Subject;
if (attachment.ODataType == "#microsoft.graph.itemAttachment")
{
var attachmentRequest = graphClient.Me.MailFolders.Inbox.Messages[message.Id]
.Attachments[attachment.Id]
.Request()
.Expand("microsoft.graph.itemattachment/item")
.GetAsync();
var itemAttachment = (ItemAttachment)attachmentRequest.Result;
var itemMessage = (Message)itemAttachment.Item;
}
}
这是我实施的解决方案。