azure Microsoft.Graph.Models.ODataErrors.尝试按ID获取消息时出现ODataError,Microsoft Graph GraphServiceClient

envsm3lx  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(98)

当我尝试使用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["email@account.com"].MailFolders["inbox"].Messages.GetAsync(); 
//messages returns 3 emails  
foreach (var message in messages.Value)
{
    //this throws the error---------------------------------------
    var messageStream = await graphServiceClient
                  .Users["email@account.com"]
                  .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解决了问题。

rt4zxlrg

rt4zxlrg1#

  • 我已经按照要求试过了,我也面临着你得到的同样的错误。

x1c 0d1x的数据

  • 然后,我更新了代码,请检查下面。
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文件成功下载到下面给定的路径


参考文献:

相关问题