简体   繁体   English

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

[英]C# - Add contact in Office 365 - Forbidden error 403

I want to add a new contact in Office 365 to use with outlook mail using C#. 我想在Office 365中添加一个新联系人以与使用C#的Outlook邮件一起使用。 I have created below code to add contact in Office 365 我创建了以下代码以在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);

Above code is not working for me. 上面的代码对我不起作用。 I am getting 403 forbidden error in response and I am not able to create contact in office 365. 我在响应中收到403禁止错误,并且无法在Office 365中创建联系人。

I don't think Office 365 accepts http basic authentication, it would explain why you are getting 403 forbidden as a response. 我认为Office 365不接受HTTP基本身份验证,它可以解释为什么您收到403禁止作为响应。

Try authenticating via OAuth instead. 尝试改为通过OAuth进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM