Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

huangapple go评论66阅读模式
英文:

Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

问题

当我尝试使用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返回3封邮件
foreach (var message in messages.Value)
{
    // 这里引发了错误---------------------------------------
    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解决了问题。

英文:

When I try to download an email from a shared mailbox using the GraphServiceClient I keep getting
Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.
The OData request is not supported.
I've googled the issue and haven't gotten a usable solution.

Originally started with app permission Mail.ReadWrite. From there added Mail.Read, Mail.ReadBasic,Mail.ReadBasic.All to see if anything would work. Below is a sample of the code that I'm using.

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);
    }
}

If there is any gotcha's as far as Azure configurations you can think of that would be appreciated.

Update:
As per the answer below, removing .MailFolders["inbox"] from the message request and adding in TokenCredentialOptions fixed the issue.

答案1

得分: 2

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

Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

  • 然后,我更新了代码,请查看下方。
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("电子邮件消息下载成功。");
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"访问 Graph API 时出错:{ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误:{ex.Message}");
            }
        }
    }
}
  • 当我执行上述代码时,.eml 文件成功下载到以下指定路径。

Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

参考资料:

英文:
  • I had tried as per the requirement, and I also faced the same error which you get.

Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

  • Then, I updated the code please check below.
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}");
            }
        }
    }
}
  • When I executed the above code the .eml file download successful to the given path below

Microsoft.Graph.Models.ODataErrors.ODataError when trying to get message by Id. Microsoft Graph GraphServiceClient

References:

huangapple
  • 本文由 发表于 2023年7月11日 04:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定