我正在尝试创建一个可以预订会议的机器人。为了做到这一点,我需要访问员工的日历以获得FreeBusy信息以最终预订会议。我试图避免硬编码电子邮件和密码,为此我想使用Azure AD中的访问令牌来调用EWS。 我设置了
的属性public static ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
使用此方法:
public static async System.Threading.Tasks.Task UseExchangeService(IDialogContext context, string userEmailAddress, SecureString userPassword)
{
string authority = ConfigurationManager.AppSettings["authority"];
string clientID = ConfigurationManager.AppSettings["clientID"];
string resource = ConfigurationManager.AppSettings["resource"];
string appKey = ConfigurationManager.AppSettings["appkey"];
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
ClientCredential clientCred = new ClientCredential(clientID, appKey);
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resource, clientCred);
service.Url = new Uri(ConfigurationManager.AppSettings["serverName"] + "/ews/exchange.asmx");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.Credentials = new OAuthCredentials(authenticationResult.AccessToken);
// USING THIS LINE IT WORKS FINE!
// service.Credentials = new NetworkCredential(userEmailAddress, userPassword); // VIRKER
}
我从Azure AD获取访问令牌,并且我已在Azure AD中授予该应用程序的权限。
我使用这种方法来提取freebusytimes,它包含其他更多的代码来显示时间作为herocard上的按钮,但这是对EWS的调用:
List<AttendeeInfo> attendees = new List<AttendeeInfo>();
attendees.Add(new AttendeeInfo()
{
SmtpAddress = "MyEMAIL",
AttendeeType = MeetingAttendeeType.Organizer
});
attendees.Add(new AttendeeInfo()
{
SmtpAddress = BookersEmail,
AttendeeType = MeetingAttendeeType.Required
});
//DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.Ad, 7, 0, 0)
// Specify options to request free/busy information and suggested meeting times.
AvailabilityOptions availabilityOptions = new AvailabilityOptions();
availabilityOptions.GoodSuggestionThreshold = 49;
availabilityOptions.MaximumNonWorkHoursSuggestionsPerDay = 0;
availabilityOptions.MaximumSuggestionsPerDay = 20;
// Note that 60 minutes is the default value for MeetingDuration, but setting it explicitly for demonstration purposes.
availabilityOptions.MeetingDuration = 60;
availabilityOptions.MinimumSuggestionQuality = SuggestionQuality.Excellent;
//TimeWindow hej = new TimeWindow();
DateTime StartDay = DateTime.Now.AddDays(1);
TimeSpan ts = new TimeSpan(9, 0, 0);
DateTime StartTime = StartDay.Date + ts;
availabilityOptions.DetailedSuggestionsWindow = new TimeWindow(StartTime, DateTime.Now.AddDays(4));
availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;
// Return free/busy information and a set of suggested meeting times.
// This method results in a GetUserAvailabilityRequest call to EWS.
GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
availabilityOptions.DetailedSuggestionsWindow,
AvailabilityData.FreeBusyAndSuggestions,
availabilityOptions);
我在Azure AD中创建了应用程序,并且我已授予以下权限: Office 365 Exchange Online:
使用对所有邮箱具有完全访问权限的Exchange Web服务
在所有邮箱中读写日历
读写用户和共享日历
通过Exchange Web服务以登录用户身份访问邮箱
我已尝试过在stackoverflow上找到的其他答案,但是他们并没有为我做这个伎俩。 希望你能帮忙
答案 0 :(得分:1)
我不熟悉EWS,但据我所知,Microsoft Graph还提供了类似的功能,可以使用下面的其余内容查找可用的会议时间(请参阅here):
POST /me/findMeetingTimes
如果您希望将此REST用于Web应用程序,以便您的Web应用程序可以委派登录用户执行Exchange操作,我们可以使用 OAuth 2.0代码授权流程 。有关如何使用此流程与Microsoft Graph集成,您可以参考以下链接:
Get started with Microsoft Graph in an ASP.NET 4.6 MVC app
以下是此流程的详细信息:
Authorize access to web applications using OAuth 2.0 and Azure Active Directory
希望它有用。