问题
I'm trying to to update email category and also mark it as read after that with the help of Outlook 365 API
and HttpClient
. Following this tutorial.
In the tutorial the code is as below to update category and mark as read but, I'm not getting that how should I attach these details to HttpClient
and request.
PATCH https://outlook.office.com/api/v2.0/me/messages/AAMkAGE0Mz8S-AAA=
Content-Type: application/json
{
"Categories": [
"Orange category",
"Green category"
],
"IsRead": true
}
The method and HttpClient I'm using are as below:
Update 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;
}
It's throwing Bad Request
error.
I am using 365 API with a WPF application. Please advise.
回答1:
Finally got solution. Here it is for newbies like me.
What was the error -
When I request API with PATCH with body I was getting Bad Request
error.
What I was doing wrong -
When I evaluated my request with this awesome tool for testing outlook 365 API, I come to know the error which was
"error": {
"code": "RequestBodyRead",
"message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected."
}
This error meant I was sending wrong data. I was sending string
to a List<string>
. This is really silly I know but it happens right? ;)
So, to correct this instead of passing JSON as fixed string, I created a class with a List<string>
property as below to have flexibility to input category as want.
public class ChangeEmailCategory
{
public List<string> Categories { get; set; }
}
And here is final method.
//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;
}
Here is method call.
List<string> categories = new List<string>();
categories.Add("Checking");
//utility is my class containing method
utility.UpdateCategory(result, categoryChangeUri, categories);
That's all! It took me one day to learn and figure it out. Thanks to all posts on Stack Overflow and google which I referred but not remember not to mention here.
If anyone need any additional information regarding this do let me know. Just mention me in comments. I will try to help.
来源:https://stackoverflow.com/questions/42808408/getting-bad-request-error-while-updating-email-category-with-office-365-api-and