在C#中使用Office 365 API和HttpClient更新电子邮件类别时出现错误请求错误 [英] Getting Bad Request error while updating email category with Office 365 API and HttpClient in C#
问题描述
我正在尝试通过Outlook 365 API
和HttpClient
来更新电子邮件类别并将其标记为已读.遵循本教程.>
在本教程中,代码如下,以更新类别并将其标记为已读,但是我没有得到应该如何将这些详细信息附加到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
错误.
我正在将365 API与WPF应用程序一起使用.请告知.
最终得到了解决方案.这是给像我这样的新手使用的.
出了什么问题-
当我向主体请求带有PATCH的API时,出现了Bad Request
错误.
我在做什么错-
当我使用此很棒的工具评估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上的所有帖子.
如果有人需要任何与此有关的其他信息,请告诉我.请在评论中提及我.我会尽力帮助.
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.
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.
这篇关于在C#中使用Office 365 API和HttpClient更新电子邮件类别时出现错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!