我得到了Error 401 Not Authorized
:
我已经配置了所有内容,下面的代码正在通过Button运行:
//Karim
string postData = "{\"Subject\": \"Discuss the Calendar REST API\",\"Body\": {\"ContentType\": \"HTML\",\"Content\": \"I think it will meet our requirements!\"},\"Start\": {\"DateTime\": \"2016 - 02 - 02T18: 00:00\",\"TimeZone\": \"Pacific Standard Time\"},\"End\": {\"DateTime\": \"2016 - 02 - 02T19: 00:00\",\"TimeZone\": \"Pacific Standard Time\"}}";
string Url = "https://outlook.office.com/api/v2.0/me/events";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);
myHttpWebRequest.Method = "POST";
byte[] data = Encoding.ASCII.GetBytes(postData);
myHttpWebRequest.ContentType = "application/json";
myHttpWebRequest.Accept = "application/json";
myHttpWebRequest.ContentLength = data.Length;
Stream requestStream = myHttpWebRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream responseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
this.Label1.Text = myStreamReader.ReadToEnd();
myStreamReader.Close();
responseStream.Close();
myHttpWebResponse.Close();
//End Karim
我已经配置了Office 365开发人员工具NuGet,我正在使用Visual Studio 2015。
必须对请求进行身份验证和授权,才能请求这些API。的确,如果您不提供代表您的请求的任何信息,谁将是“ / me”?
为此,您应该设置一个所谓的身份验证载体,在您的C#.NET调用中它将看起来像这样。
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "<your access token as string here>");
要了解如何检索此类访问令牌,请从本文档开始。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句