C# - 在Office 365中添加联系人 - 禁止错误403

时间:2017-01-22 18:55:40

标签: c# asp.net asp.net-mvc httpwebrequest office365

我想在Office 365中添加一个新联系人,以便使用C#与outlook邮件一起使用。我创建了以下代码以在Office 365中添加联系人

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://outlook.office.com/api/v2.0/me/contacts"));

// Add the Authorization header with the basic login credentials.                
var auth = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes("XXXXXXXX" + ":" + "XXXXXXXX"));
request.Headers.Add("Authorization", auth);
request.Headers.Add("contact_folder_id", "test");

private const string odata = "@odata.type";
private const string type = "#Microsoft.Exchange.Services.OData.Model.Contact";

var createResponse = new JObject();
createResponse[odata] = type; // this needs to be here for this to work
createResponse["DisplayName"] = "Display Name";
createResponse["GivenName"] = "Given Name";
createResponse["MiddleName"] = "Middle Name";
createResponse["Surname"] = "Surname";
createResponse["EmailAddress1"] = "abc111@gmail.com";

request.Content = new StringContent(JsonConvert.SerializeObject(createResponse));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.SendAsync(request);

以上代码对我不起作用。我收到了403禁止错误的回复,我无法在办公室365中建立联系。

1 个答案:

答案 0 :(得分:0)

我认为Office 365不接受http基本身份验证,它可以解释为什么你将403禁止作为响应。

尝试通过OAuth进行身份验证。