[英]Office365 REST API returns Unauthorized with C# ADAL
我正在構建一個WPF-App。 首先獲取事件,然后通過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);
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響應= MainWindow.HttpClient.PostAsync(graphRequest,emailBody).Result;
您發送帖子請求的代碼將不會發送您設置的標題,因為所有標題都是為request
參數設置的,但您沒有在帖子中使用它。
要使用access_token發送帖子,您可以參考以下代碼:
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Bearer", accessToken);
httpClient.DefaultRequestHeaders.Add("Prefer", "outlook.timezone=\"W. Europe Standard Time\"");
HttpResponseMessage response = httpClient.PostAsync(graphRequest, emailBody).Result;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.