【发布时间】:2017-05-16 15:40:49
【问题描述】:
我正在构建一个 WPF 应用程序。首先获取事件,然后通过 O365 RestAPI 创建事件。
我可以通过以下方式获取事件:
result = await authContext.AcquireTokenAsync(graphResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
string today = DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture);
string graphRequest = String.Format(CultureInfo.CurrentCulture, "https://outlook.office365.com/api/v2.0/me/calendarview?startDateTime=" + today + "T00:00:00&endDateTime=" + today + "T23:59:00&$select=Subject,organizer,start,end,attendees&$orderby=start/datetime%20asc");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphRequest);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
HttpResponseMessage response = HttpClient.SendAsync(request).Result;
if (!response.IsSuccessStatusCode)
throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);
但是当我尝试创建事件时,我会收到使用相同令牌的“未经授权”。我的应用拥有读取和写入日历的权限。
这是我的代码:
string postBody = "{'Subject':" + "'Discuss the Calendar REST API'," +
"'Body':{ " +
"'ContentType':'HTML'," +
"'Content': 'I think it will meet our requirements!'},"
+ "'Start': { DateTime: '" +
start + "',"
+ " 'TimeZone': 'W. Europe Standard Time'}," +
"'End':{'DateTime': '" + end + "'," +
"'TimeZone': 'W. Europe Standard Time'},"
+ "'Attendees':[{" +
"'EmailAddress':{"
+ "'Address': '" + MailTB.Text + "'"
+
" },"
+ "'Type': 'Required'}]}";
var emailBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");
AuthenticationContext authContext = new AuthenticationContext(authority, new FileCache());
AuthenticationResult result = await authContext.AcquireTokenAsync(graphResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
string today = DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture);
string graphRequest = String.Format(CultureInfo.CurrentCulture, "https://outlook.office365.com/api/v2.0/me/events");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, graphRequest);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Headers.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
HttpResponseMessage response = MainWindow.HttpClient.PostAsync(graphRequest, emailBody).Result;
if (!response.IsSuccessStatusCode)
throw new WebException(response.StatusCode.ToString() + ": " + response.ReasonPhrase);
【问题讨论】:
-
详细的错误信息是什么?为这个问题更新令牌是否有帮助?
-
@FeiXue-MSFT 这就是我得到的:状态代码:401 未经授权的内容长度:0 日期:星期一,2017 年 5 月 15 日 14:02:51 GMT 服务器:Microsoft-IIS/8.5 WWW- Authenticate: Basic Realm="" X-FEServer: HE1PR0902CA0019 X-Powered-By: ASP.NET request-id: a95cc2b6-7e90-4fe7-8f26-24c13f570f29 我刷新了令牌,一切还是一样
标签: c# oauth-2.0 adal office365api