azure Microsoft Graph抛出“内容类型text/html没有注册要解析的工厂”

sxpgvts3  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(96)

我正在浏览微软的一个控制台应用程序教程,以处理Outlook邮件here
我照原样做了一切,但是当我尝试测试它时,当发送GET请求时,我得到了这个异常。
内容类型text/html没有要解析的注册工厂。
代码:

public static Task<MessageCollectionResponse?> GetInboxAsync()
{
    // Ensure client isn't null
    _ = _userClient ??
        throw new System.NullReferenceException("Graph has not been initialized for user auth");

    return _userClient.Me
        // Only messages from Inbox folder
        .MailFolders["Inbox"]
        .Messages
        .GetAsync((config) =>
        {
            //The next two lines were added by me to try to fix the problem
            config.Headers.Clear();
            config.Headers.Add("Accept", "application/json");
            // Only request specific properties
            config.QueryParameters.Select = new[] { "from", "isRead", "receivedDateTime", "subject" };
            // Get at most 25 results
            config.QueryParameters.Top = 25;
            // Sort by received time, newest first
            config.QueryParameters.Orderby = new[] { "receivedDateTime DESC" };
        });
}

字符串

oknwwptz

oknwwptz1#

我按照相同的link注册了一个Azure AD应用,支持的账户类型如下,并启用了公共客户端流:
x1c 0d1x的数据
我有一个名为DemoAAD的租户,其Azure AD Free许可证如下:



在我的例子中,当我使用上面租户的用户登录时运行相同的代码时,我得到了相同的错误

dotnet run
2

字符串

回复:



若要解决此错误,请将您的Azure AD许可证更新为**Premium,并确保将Office 365许可证分配给您的用户帐户。
我有另一个租户,其
Azure AD Premium P2**许可证如下:



在上面的租户中,我为一个用户帐户分配了一个活动的Office 365许可证
x1c4d 1x的
当我运行相同的代码时,我从Azure AD Premium P2许可租户中选择了具有活动Office 365许可证的用户帐户登录,在那里我得到了这样的同意提示:



同意后,输入2,控制台输出收件箱消息列表成功:

dotnet run
2

回复:


参考:c# -使用Azure MS Graph API服务帐户- Stack Overflow byRukmini

zysjyyx4

zysjyyx42#

这个错误Content type text/html does not have a factory registered to be parsed意味着你得到了一个html内容作为响应,它总是指示请求得到了错误。
我的建议是缩小问题范围:

public static Task<MessageCollectionResponse?> GetInboxAsync()
{
    // Ensure client isn't null
    _ = _userClient ??
        throw new System.NullReferenceException("Graph has not been initialized for user auth");

    var a = await _userClient.Me.GetAsync();//test if graph Client is set correctly
    //checking if permission is enough to query messages and if the user account have Email account binding to user account
    var b = await _userClient.Me.Messages.GetAsync((requestConfiguration) =>
            {
                requestConfiguration.QueryParameters.Select = new string []{ "sender","subject" };
            });
    //add break point here to see the error message.
    var res = await _userClient.Me
        .MailFolders["Inbox"]
        .Messages
        .GetAsync((config) =>
        {
            //The next two lines were added by me to try to fix the problem
            //config.Headers.Clear();
            //config.Headers.Add("Accept", "application/json");
            // Only request specific properties
            config.QueryParameters.Select = new[] { "from", "isRead", "receivedDateTime", "subject" };
            // Get at most 25 results
            //config.QueryParameters.Top = 25;
            config.QueryParameters.Orderby = new[] { "receivedDateTime DESC" };
        });
    return res;
}

字符串

相关问题