Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中使用Office 365 API和HttpClient更新电子邮件类别时出现错误请求#_C#_Wpf_Httprequest_Dotnet Httpclient_Office365api - Fatal编程技术网

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

C# 在C中使用Office 365 API和HttpClient更新电子邮件类别时出现错误请求#,c#,wpf,httprequest,dotnet-httpclient,office365api,C#,Wpf,Httprequest,Dotnet Httpclient,Office365api,我正在尝试更新电子邮件类别,并在outlook365 API和HttpClient的帮助下将其标记为read。跟着 在本教程中,代码如下所示,用于更新类别并标记为已读,但我不明白如何将这些详细信息附加到HttpClient并请求 PATCH https://outlook.office.com/api/v2.0/me/messages/AAMkAGE0Mz8S-AAA= Content-Type: application/json { "Categories": [ &qu

我正在尝试更新电子邮件类别,并在
outlook365 API
HttpClient
的帮助下将其标记为read。跟着

在本教程中,代码如下所示,用于更新类别并标记为已读,但我不明白如何将这些详细信息附加到
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;
    }
它抛出了
错误请求
错误


我正在将365 API与WPF应用程序一起使用。请告知。

最终得到了解决方案。这是给像我这样的新手的

错误是什么-

当我用带有主体的补丁请求API时,我得到了
错误请求
错误

我做错了什么-

当我使用用于测试Outlook365 API的很棒的工具评估我的请求时,我知道了错误是什么

"error": {
    "code": "RequestBodyRead",
    "message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected."
}
这个错误意味着我发送了错误的数据。我正在将
字符串
发送到
列表
。我知道这真的很傻,但它发生得对吗?;)

因此,为了纠正这一点,而不是将JSON作为固定字符串传递,我创建了一个具有
List
属性的类,如下所示,以便能够灵活地根据需要输入类别

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

}
公共类更改电子邮件类别
{
公共列表类别{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;
    }
//传递参数-AuthenticationResult、带有身份验证头的URI、类别列表。
公共字符串更新类别(AuthenticationResult、字符串URI、列表类别)
{
//HTTPMethod.PATCH不可用,因此需要手动添加。
var httpMethod=新的httpMethod(“补丁”);
HttpRequestMessage请求=新的HttpRequestMessage(httpMethod,uriString);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,result.AccessToken);
ChangeEmailCategory cec=新的ChangeEmailCategory();
cec.类别=类别;
//将类属性序列化为JSON
var jsonData=JsonConvert.SerializeObject(cec);
//向请求体添加JSON
request.Content=newstringcontent(jsonData,Encoding.UTF8,“application/json”);
HttpResponseMessage response=httpClient.SendAsync(请求).Result;
如果(!response.issucessStatusCode)
抛出新的WebException(response.StatusCode.ToString()+”:“+response.ReasonPhrase);
返回response.Content.ReadAsStringAsync().Result;
}
这里是方法调用

List<string> categories = new List<string>();
categories.Add("Checking");
//utility is my class containing method
utility.UpdateCategory(result, categoryChangeUri, categories);
列表类别=新列表();
类别。添加(“检查”);
//实用程序是我的类包含的方法
UpdateCategory(结果、类别更改、类别);
就这些!我花了一天的时间才学会并弄明白。感谢所有关于Stack Overflow和google的帖子,我提到了它们,但不记得在这里没有提到


如果有人需要关于这方面的任何其他信息,请告诉我。只是在评论中提到我。我会尽力帮忙。

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

错误是什么-

当我用带有主体的补丁请求API时,我得到了
错误请求
错误

我做错了什么-

当我使用用于测试Outlook365 API的很棒的工具评估我的请求时,我知道了错误是什么

"error": {
    "code": "RequestBodyRead",
    "message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected."
}
这个错误意味着我发送了错误的数据。我正在将
字符串
发送到
列表
。我知道这真的很傻,但它发生得对吗?;)

因此,为了纠正这一点,而不是将JSON作为固定字符串传递,我创建了一个具有
List
属性的类,如下所示,以便能够灵活地根据需要输入类别

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

}
公共类更改电子邮件类别
{
公共列表类别{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;
    }
//传递参数-AuthenticationResult、带有身份验证头的URI、类别列表。
公共字符串更新类别(AuthenticationResult、字符串URI、列表类别)
{
//HTTPMethod.PATCH不可用,因此需要手动添加。
var httpMethod=新的httpMethod(“补丁”);
HttpRequestMessage请求=新的HttpRequestMessage(httpMethod,uriString);
request.Headers.Authorization=新的AuthenticationHeaderValue(“承载者”,result.AccessToken);
ChangeEmailCategory cec=新的ChangeEmailCategory();
cec.类别=类别;
//将类属性序列化为JSON
var jsonData=JsonConvert.SerializeObject(cec);
//向请求体添加JSON
request.Content=newstringcontent(jsonData,Encoding.UTF8,“application/json”);
HttpResponseMessage response=httpClient.SendAsync(请求).Result;
如果(!response.issucessStatusCode)
抛出新的WebException(response.StatusCode.ToString()+”:“+response.ReasonPhrase);
返回response.Content.ReadAsStringAsync().Result;
}
这里是方法调用

List<string> categories = new List<string>();
categories.Add("Checking");
//utility is my class containing method
utility.UpdateCategory(result, categoryChangeUri, categories);
列表类别=新列表();
类别。添加(“检查”);
//实用程序是我的类包含的方法
UpdateCategory(结果、类别更改、类别);
就这些!我花了一天的时间才学会并弄明白。感谢所有关于Stack Overflow和google的帖子,我提到了它们,但不记得在这里没有提到

如果有人需要关于这方面的任何其他信息,请告诉我。只是在评论中提到我。我会尽力帮忙的