简体   繁体   English

EWS Oauth异常:请求失败。 远程服务器返回错误:(401)未经授权

[英]EWS Oauth Exception: The request failed. The remote server returned an error: (401) Unauthorized

I'm trying to create a bot that can book meetings. 我正在尝试创建一个可以预定会议的机器人。 In order to do that i need to access the calendar of an employee to get FreeBusy info to ultimately book a meeting. 为此,我需要访问员工的日历以获取FreeBusy信息以最终预订会议。 I'm trying to avoid hardcoding the email and password and for that I want to use an access token from Azure AD to call EWS. 我试图避免对电子邮件和密码进行硬编码,为此,我想使用Azure AD中的访问令牌来调用EWS。 I set the properties for 我为设置属性

public static ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);

using this method: 使用此方法:

       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

    }

I do get the access token from Azure AD and I have granted the permission for the application in Azure AD. 我确实从Azure AD获取了访问令牌,并且已经为Azure AD中的应用程序授予了权限。

I use this method to extract the freebusytimes, it contains other more code to display the times as buttons on a herocard, but this is the call to EWS: 我使用这种方法提取忙闲时间,它包含其他更多代码以将时间显示为英雄卡上的按钮,但这是对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);

I have created the application in Azure AD and I have granted the following permissions: Office 365 Exchange Online: 我已经在Azure AD中创建了应用程序,并授予了以下权限:Office 365 Exchange Online:

Use Exchange Web Services with full access to all mailboxes 使用对所有邮箱具有完全访问权限的Exchange Web服务

Read and write calendars in all mailboxes 在所有邮箱中读取和写入日历

Read and write user and shared calendars 读写用户和共享日历

Access mailboxes as the signed-in user via Exchange Web Services 通过Exchange Web Services以登录用户身份访问邮箱

I've tried other answers i found on stackoverflow, however they do not do the trick for me. 我尝试了在stackoverflow上找到的其他答案,但是它们对我没有帮助。 Hope You can help 希望你能帮忙

I am not familiar with EWS, however as far as I know that the Microsoft Graph also provide the similar feature for find the available meeting time using the rest below( refer here ): 我不熟悉EWS,但是据我所知,Microsoft Graph还提供了类似的功能,可以使用下面的其余部分(请参阅此处 )查找可用的会议时间:

POST /me/findMeetingTimes

And if you want to using this REST for the web application so that your web app can delegate the sign-in user to perform the operation for Exchange, we can use the OAuth 2.0 code grant flow . 而且,如果您想将此REST用于Web应用程序,以便您的Web应用程序可以委派登录用户执行Exchange的操作,则可以使用OAuth 2.0代码授予流程 And for how to use this flow to integrate with Microsoft Graph, you can refer the links below: 有关如何使用此流程与Microsoft Graph集成的方法,可以参考以下链接:

Get started with Microsoft Graph in an ASP.NET 4.6 MVC app 在ASP.NET 4.6 MVC应用程序中开始使用Microsoft Graph

And here is the detail for this flow: 这是此流程的详细信息:

Authorize access to web applications using OAuth 2.0 and Azure Active Directory 使用OAuth 2.0和Azure Active Directory授权对Web应用程序的访问

Hope it is helpful. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预警系统。 请求失败。 远程服务器返回错误:(401) Unauthorized on Mac OS only - EWS. The request failed. The remote server returned an error: (401) Unauthorized on Mac OS only 请求失败。 远程服务器返回错误:(401)连接到Office365时出现未授权错误 - The request failed. The remote server returned an error: (401) Unauthorized error while connecting to Office365 EWS:“远程服务器返回错误(401)未经授权” - EWS: “The remote server returned error (401) Unauthorized” 远程服务器返回错误:(401) Unauthorized。 Twitter oAuth - The remote server returned an error: (401) Unauthorized. Twitter oAuth 错误远程服务器返回错误:(401)未经授权 - Error The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized 远程服务器返回错误:(401)未授权的sharepoint - The remote server returned an error: (401) Unauthorized sharepoint 远程服务器返回错误:(401)未经授权 - The remote server returned an error: (401) Unauthorized Web 异常:远程服务器返回错误 (401) 未授权 - Webexception:remote server returned an error (401)unauthorized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM