【问题标题】:EWS Oauth Exception: The request failed. The remote server returned an error: (401) UnauthorizedEWS Oauth 异常:请求失败。远程服务器返回错误:(401) Unauthorized
【发布时间】:2017-02-17 09:13:35
【问题描述】:

我正在尝试创建一个可以预订会议的机器人。为此,我需要访问员工的日历以获取 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 中应用程序的权限。

我使用这种方法来提取空闲时间,它包含其他更多代码来将时间显示为 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 Web 服务

在所有邮箱中读写日历

读写用户和共享日历

通过 Exchange Web 服务以登录用户身份访问邮箱

我已经尝试过在 stackoverflow 上找到的其他答案,但是它们对我没有用。 希望你能帮忙

【问题讨论】:

  • 你能用问题编辑你的帖子吗?我不确定你在问什么。

标签: c# exchangewebservices azure-active-directory botframework


【解决方案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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    相关资源
    最近更新 更多