在 C# 中使用 Office 365 API 和 HttpClient 更新电子邮件类别时出现错误请求错误

Getting Bad Request error while updating email category with Office 365 API and HttpClient in C#

我正在尝试更新电子邮件类别,然后在 Outlook 365 APIHttpClient 的帮助下将其标记为已读。关注 this tutorial.

在本教程中,代码如下,用于更新类别并标记为已读,但我不知道如何将这些详细信息附加到 HttpClient 和请求。

PATCH https://outlook.office.com/api/v2.0/me/messages/AAMkAGE0Mz8S-AAA=
Content-Type: application/json

{
"Categories": [
"Orange category",
"Green category"
],
"IsRead": true
}

我使用的方法和HttpClient如下:

更新 1

public string UpdateCategory(AuthenticationResult result, string mediator)
    {
    //HTTPMethod.PATCH not available to adding it manualy.
    var httpMethod = new HttpMethod("PATCH");
    HttpRequestMessage request = new HttpRequestMessage(httpMethod, mediator);
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    //JSON in a string variable for test
    var tempJson = @"{""Categories"" : ""Checking""}";
    Converting string to JSON
    var jsonData = JsonConvert.SerializeObject(tempJson);
    //Adding the JSON to request.Content
    request.Content =new StringContent(jsonData,Encoding.UTF8, "application/json");
    HttpResponseMessage response = httpClient.SendAsync(request).Result;
    if (!response.IsSuccessStatusCode)
     throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);
    mediator = response.Content.ReadAsStringAsync().Result;
    return mediator;
    }

正在抛出 Bad Request 错误。

我在 WPF 应用程序中使用 365 API。请指教

终于找到解决办法了。这是给像我这样的新手的。

错误是什么-

当我请求 API 带有正文的 PATCH 时,我收到 Bad Request 错误。

我做错了什么-

当我使用 this 很棒的 outlook 365 测试工具 API 评估我的请求时,我发现错误是

"error": {
    "code": "RequestBodyRead",
    "message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected."
}

这个错误意味着我发送了错误的数据。我正在发送 stringList<string>。我知道这真的很愚蠢,但它发生了吗? ;)

因此,为了更正此问题而不是将 JSON 作为固定字符串传递,我创建了一个 class 和 List<string> 属性 如下所示,以便可以灵活地输入类别随心所欲。

public class ChangeEmailCategory
{
    public List<string> Categories { get; set; }

}

这是最终方法。

//Passing parameters - AuthenticationResult, URI with authentication header, List of categories.
public string UpdateCategory(AuthenticationResult result, string uriString,List<string> categories)
    {
        //HTTPMethod.PATCH not available so adding it manualy.
        var httpMethod = new HttpMethod("PATCH");
        HttpRequestMessage request = new HttpRequestMessage(httpMethod, uriString);
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
        ChangeEmailCategory cec = new ChangeEmailCategory();
        cec.Categories = categories;
        //Serializing class properties as JSON
        var jsonData = JsonConvert.SerializeObject(cec);
        //Adding JSON to request body
        request.Content =new StringContent(jsonData,Encoding.UTF8, "application/json");
        HttpResponseMessage response = httpClient.SendAsync(request).Result;
        if (!response.IsSuccessStatusCode)
            throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);
        return response.Content.ReadAsStringAsync().Result;
    }

这里是方法调用。

List<string> categories = new List<string>();
categories.Add("Checking");
//utility is my class containing method
utility.UpdateCategory(result, categoryChangeUri, categories);

就是这样!我花了一天的时间来学习和弄明白。感谢 Stack Overflow 和 google 上的所有帖子,我提到了但不记得在这里没有提及。

如果有人需要有关此的任何其他信息,请告诉我。请在评论中提及我。我会尽力帮忙的。