gpt4 book ai didi

c# - 尝试通过 ID 获取消息时出现 Microsoft.Graph.Models.ODataErrors.ODataError 错误。 Microsoft Graph GraphServiceClient

转载 作者:行者123 更新时间:2023-12-02 06:35:09 26 4
gpt4 key购买 nike

当我尝试使用 GraphServiceClient 从共享邮箱下载电子邮件时,我不断收到引发了“Microsoft.Graph.Models.ODataErrors.ODataError”类型的异常。不支持 OData 请求。我用谷歌搜索了这个问题,但没有找到可用的解决方案。

最初以应用程序权限 Mail.ReadWrite 启动。从那里添加 Mail.Read、Mail.ReadBasic、Mail.ReadBasic.All 以查看是否有任何功能。下面是我正在使用的代码示例。

ClientSecretCredential? _clientSecretCredential = new ClientSecretCredential(_settings.TenantId, _settings.ClientId, _settings.ClientSecret);
var graphServiceClient = new GraphServiceClient(_clientSecretCredential,new[] { "https://graph.microsoft.com/.default" });
var messages = await graphServiceClient.Users["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffaf2fef6f3dffefcfcf0eaf1ebb1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>"].MailFolders["inbox"].Messages.GetAsync();
//messages returns 3 emails
foreach (var message in messages.Value)
{
//this throws the error---------------------------------------
var messageStream = await graphServiceClient
.Users["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32575f535b5e725351515d475c461c515d5f" rel="noreferrer noopener nofollow">[email protected]</a>"]
.MailFolders["inbox"]
.Messages[message.Id]
.Content
.GetAsync();
//---------------------------------------------------------------
string path = "File_Path.eml";
using (FileStream fs = new FileStream(path, FileMode.CreateNew))
{
messageStream.CopyTo(fs);
}
}

如果您能想到 Azure 配置方面存在任何问题,我们将不胜感激。

更新:根据下面的答案,从消息请求中删除 .MailFolders["inbox"] 并添加 TokenCredentialOptions 解决了该问题。

最佳答案

  • 我已按照要求进行了尝试,但我也遇到了与您相同的错误。

enter image description here

  • 然后,我更新了代码,请查看下面。
using Azure.Identity;
using Microsoft.Graph;
using System;
using System.IO;

namespace GraphApp
{
class MSGraph
{
static async Task Main(string[] args)
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_Id";
var clientId = "client_Id";
var clientSecret = "client_Secret";
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

try
{
var message = await graphClient.Users["User_UPN"]
.MailFolders["inbox"]
.Messages["message_ID"]
.GetAsync();

var mimeContentStream = await graphClient.Users["User_ID"]
.Messages[message.Id]
.Content
.GetAsync();

using (var fileStream = File.Create("C:\\Users\\xxxxxxx\\Desktop\\web1\\lastemail.eml"))
{
mimeContentStream.Seek(0, SeekOrigin.Begin);
await mimeContentStream.CopyToAsync(fileStream);
}

Console.WriteLine("Email message downloaded successfully.");
}
catch (ServiceException ex)
{
Console.WriteLine($"Error accessing the Graph API: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
  • 当我执行上述代码时,.eml 文件成功下载到下面的给定路径

enter image description here

引用文献:

关于c# - 尝试通过 ID 获取消息时出现 Microsoft.Graph.Models.ODataErrors.ODataError 错误。 Microsoft Graph GraphServiceClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76656982/

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