Office365 REST API returns 未经 C# ADAL 授权
Office365 REST API returns Unauthorized with C# ADAL
我正在构建一个 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);
但是当我尝试创建事件时,我会收到一个使用相同令牌的 "Unauthorized"。我的应用程序具有读取和写入日历的权限。
这是我的代码:
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 response = MainWindow.HttpClient.PostAsync(graphRequest, emailBody).Result;
您发送 post 请求的代码不会发送您设置的 headers,因为所有 headers 都是为 request
参数设置的,但您没有在 post 中使用它。
要发送post和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;
我正在构建一个 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);
但是当我尝试创建事件时,我会收到一个使用相同令牌的 "Unauthorized"。我的应用程序具有读取和写入日历的权限。
这是我的代码:
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 response = MainWindow.HttpClient.PostAsync(graphRequest, emailBody).Result;
您发送 post 请求的代码不会发送您设置的 headers,因为所有 headers 都是为 request
参数设置的,但您没有在 post 中使用它。
要发送post和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;