【发布时间】:2017-03-15 11:25:01
【问题描述】:
我正在尝试更新电子邮件类别,并在 Outlook 365 API 和 HttpClient 的帮助下将其标记为已读。关注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