2017-03-15 27 views
1

我試圖更新電子郵件類別,並在Outlook 365 APIHttpClient的幫助下將其標記爲已讀。關注this tutorial使用Office 365 API和HttpClient在C#中更新電子郵件類別時出現錯誤請求錯誤

在教程中,代碼如下更新類別並標記爲已讀,但是,我不明白我應如何將這些詳細信息附加到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應用程序。請指教。

回答

0

終於有了解決辦法。這裏是像我這樣的新手。

什麼是錯誤 - 當我請求API與身體我正在Bad Request錯誤PATCH

我在做什麼錯 -

當我評估我的要求與測試展望365 API this真棒工具,我才知道這是錯誤

"error": { 
    "code": "RequestBodyRead", 
    "message": "An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartArray' node was expected." 
} 

此錯誤意味着我發送錯誤的數據。我發送stringList<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提到的所有帖子,但不記得這裏不提。

如果有人需要關於此的任何額外信息,請讓我知道。請在評論中提及我。我會盡力幫助。

相關問題