Office 365 REST API - 创建联系人为我提供了HTTPCode 400

时间:2014-07-06 14:13:37

标签: c# .net api rest office365

我正在使用Office 365 Rest API,我在创建联系人时遇到了问题:

我这样做:

public async Task<bool> CreateContact(Contact contact)
    {
        var client = new HttpClient();
        var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://outlook.office365.com/ews/odata/Me/Contacts"));

        // Add the Authorization header with the basic login credentials.
        var auth = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(_user + ":" + _password));
        request.Headers.Add("Accept", "application/json");
        request.Headers.Add("Authorization", auth);
        var createResponse = new JObject();
        createResponse["@odata.type"] = "#Microsoft.Exchange.Services.OData.Model.Contact";
        createResponse["DisplayName"] = contact.Name;
        createResponse["EmailAddress1"] = contact.Email;
        request.Content = new StringContent(JsonConvert.SerializeObject(createResponse), Encoding.UTF8, "application/json;odata.metadata=full");

        var response = await client.SendAsync(request);
        if (response.IsSuccessStatusCode)
        {
            return true;
        }
        return false;
    }

给我

{System.FormatException: The format of value 'application/json;odata.metadata=full' is invalid. at System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName) at System.Net.Http.Headers.MediaTypeHeaderValue..ctor(String mediaType) at System.Net.Http.StringContent..ctor(String content, Encoding encoding, String mediaType) at SharePointPTSample.Office365.Office365Service.d__11.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at SharePointPTSample.ViewModels.EditContactViewModel.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(Object state)}

但如果我将值“application / json; odata.metadata = full”更改为“application / json”, 它给了我400

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 0.0, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: private Server: Microsoft-IIS/8.0 request-id: 3679d732-ae03-4358-b256-3738cbf24030 X-CalculatedBETarget: dbxpr06mb352.eurprd06.prod.outlook.com X-DiagInfo: DBXPR06MB352 X-BEServer: DBXPR06MB352 X-AspNet-Version: 4.0.30319 Set-Cookie: exchangecookie=daa5b4f2b05b422f8c5e99105e617429; expires=Sun, 05-Jul-2015 22:06:47 GMT; path=/; HttpOnly, X-BackEndCookie=< removed it for post>; expires=Mon, 04-Aug-2014 22:06:47 GMT; path=/ews; secure; HttpOnly X-Powered-By: ASP.NET X-FEServer: AMXPR05CA0040 Date: Sat, 05 Jul 2014 22:06:47 GMT Content-Length: 567 Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; IEEE754Compatible=false; charset=utf-8 }}

我关注此链接

http://msdn.microsoft.com/en-us/library/office/dn605896(v=office.15).aspx#bkContact

缺少什么?我没有承诺:/

我有删除,更新和GetContacts工作。

完整的源代码在这里: https://github.com/saramgsilva/Office365RESTAPISample

1 个答案:

答案 0 :(得分:2)

saramgsilva, 感谢您试用API。 我尝试了你的代码和一些东西。 1.您需要将ContentType标头指定为application / json。将odata部分留下。 你提出之后,你会看到下一个问题,即需要名字。一旦你提供这两个,你应该得到201。

        createResponse["GivenName"] = "Rohit1";
        request.Content = new StringContent(JsonConvert.SerializeObject(createResponse));
        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");