【问题标题】:Getting Bad Request error while updating email category with Office 365 API and HttpClient in C#在 C# 中使用 Office 365 API 和 HttpClient 更新电子邮件类别时出现错误请求错误
【发布时间】:2017-03-15 11:25:01
【问题描述】:

我正在尝试更新电子邮件类别,并在 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。请指教。

【问题讨论】:

    标签: c# wpf httprequest dotnet-httpclient office365api


    【解决方案1】:

    终于解决了。这是给像我这样的新手的。

    错误是什么 -

    当我使用 PATCH 请求 API 时,我收到了 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."
    }
    

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

    因此,为了纠正这个问题,而不是将 JSON 作为固定字符串传递,我创建了一个具有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 上的所有帖子,我提到但不记得在这里不提了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2020-04-17
      • 1970-01-01
      • 2020-03-02
      • 2015-04-05
      • 1970-01-01
      相关资源
      最近更新 更多