我正在制定一项要求,即创建一个Windows服务,该服务将日历邀请发送到Office365上托管邮件服务器的管理员电子邮件地址的预定会议。我创建了一个Azure应用程序,并且能够获取访问令牌,当我尝试从剩余服务中读取日历事件时,内部服务器异常发生了#34;。 Follwing是我试过的代码片段。请你帮我/推荐我的资源,以达到我的要求。
private static string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
private static string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
private static string appKey = ConfigurationManager.AppSettings["ida:AppKey"];
static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
private static HttpClient httpClient = new HttpClient();
private static AuthenticationContext authContext = null;
private static ClientCredential clientCredential = null;
public static void CalendarAPICall()
{
authContext = new AuthenticationContext(authority);
clientCredential = new ClientCredential(clientId, appKey);
AuthenticationResult result = null;
int retryCount = 0;
bool retry = false;
string header = "";
do
{
retry = false;
try
{
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired.
result = authContext.AcquireToken(todoListResourceId, clientCredential);
header = result.CreateAuthorizationHeader();
}
catch (AdalException ex)
{
if (ex.ErrorCode == "temporarily_unavailable")
{
retry = true;
retryCount++;
Thread.Sleep(3000);
}
Console.WriteLine(
String.Format("An error occurred while acquiring a token\nTime: {0}\nError: {1}\nRetry: {2}\n",
DateTime.Now.ToString(),
ex.ToString(),
retry.ToString()));
}
} while ((retry == true) && (retryCount < 3));
if (result == null)
{
Console.WriteLine("Canceling attempt to contact To Do list service.\n");
return;
}
var url = "https://outlook.office365.com/api/v2./me/calendarview?startDateTime=2014-10-01T01:00:00Z&endDateTime=2015-12-31T23:00:00Z";
var request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
var oAuthHeader = header;
request.Headers.Add("Authorization", oAuthHeader);
var response = request.GetResponse();
}