简体   繁体   English

将EWS托管Api与Office 365 Api结合使用

[英]Using EWS Managed Api with Office 365 Api

I am trying to use EWS managed Api with Office 365 Api via Azure AD. 我正在尝试通过Azure AD将EWS托管的Api与Office 365 Api一起使用。 I have done the following tasks so far. 到目前为止,我已经完成了以下任务。

  • I have the admin privilege in Azure AD. 我在Azure AD中具有管理员权限。
  • I have successfully registered my application in Azure AD. 我已经在Azure AD中成功注册了我的应用程序。
  • I got Client ID, App key and resource ID from Azure AD. 我从Azure AD获得了客户端ID,应用程序密钥和资源ID。
  • I have enabled "Have full access to user's mailbox. as suggested by Jason. 我已启用“具有对用户邮箱的完全访问权限。Jason建议。
  • I have successfully created a MVC5 web application. 我已经成功创建了MVC5 Web应用程序。
  • I have followed this blog post of Jeremy. 我关注了杰里米(Jeremy)的博客文章。

Here the link of the blog I have followed : http://www.jeremythake.com/2014/08/using-the-exchange-online-ews-api-with-office-365-api-via-azure-ad/#comment-280653 这是我关注的博客链接: http : //www.jeremythake.com/2014/08/using-the-exchange-online-ews-api-with-office-365-api-via-azure-ad/ #评论-280653

Code in my controller: 我的控制器中的代码:

   var outlookClient = await AuthHelper.EnsureOutlookServicesClientCreatedAsync("Mail");

    IPagedCollection<IMessage> messagesResults = await     outlookClient.Me.Messages.ExecuteAsync();

    string messageId = messagesResults.CurrentPage[0].Id;
    string tokenx = AuthHelper.GetSessionToken();
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
    service.HttpHeaders.Add("Authorization", "Bearer " + tokenx);
    service.PreAuthenticate = true;
    service.SendClientLatencies = true;
    service.EnableScpLookup = false;
    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    ExFolder rootfolder = ExFolder.Bind(service, WellKnownFolderName.MsgFolderRoot);

Edited : I am getting accessToken Successfully and using it to make call against EWS managed Api, but it fails with 403:Forbidden exception. 编辑:我正在成功获取accessToken并使用它来对EWS托管的Api进行调用,但是失败并显示403:Forbidden异常。 Your help will be highly appreciated. 非常感谢您的帮助。

best regards, 最好的祝福,

Jason Johnston helped me solve my problem. 杰森·约翰斯顿(Jason Johnston)帮助我解决了我的问题。 The link: 链接:
Office 365 / EWS Authentication using OAuth: The audience claim value is invalid 使用OAuth的Office 365 / EWS身份验证:受众声明值无效

I checked the EWS trace, I learned that EWS was complaining about invalid token and insufficient privileges. 我检查了EWS跟踪,了解到EWS抱怨无效的令牌和特权不足。 I re-registered my application to Azure AD and enabled full access to mailbox. 我将应用程序重新注册到Azure AD,并启用了对邮箱的完全访问权限。

I commented this below code. 我在下面的代码中对此进行了评论。

//var outlookClient = await AuthHelper.EnsureOutlookServicesClientCreatedAsync("Mail");
        //try
        //{
        //    IPagedCollection<IMessage> messagesResults = await outlookClient.Me.Messages.ExecuteAsync();

        //    string messageId = messagesResults.CurrentPage[0].Id;
        //}
        //catch
        //{
        //    System.Diagnostics.Debug.WriteLine("Something bad happened. !!");
        //}  

I am getting access token from this below link sample. 我从下面的链接示例中获取访问令牌。 https://github.com/OfficeDev/Office-365-APIs-Starter-Project-for-ASPNETMVC https://github.com/OfficeDev/Office-365-APIs-Starter-Project-for-ASPNETMVC

Here is the complete code of controller which does the main task of authentication. 这是完成身份验证主要任务的控制器的完整代码。

string resourceUri = "https://outlook.office365.com";
        var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
        AuthenticationContext authContext = new AuthenticationContext(Settings.Authority, new NaiveSessionCache(signInUserId));
        string tokenx = await AuthHelper.AcquireTokenAsync(authContext, resourceUri, Settings.ClientId, new UserIdentifier(userObjectId,UserIdentifierType.UniqueId));

        System.Diagnostics.Debug.WriteLine("Token:" + tokenx);

            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
            service.TraceListener = new EwsTrace();
            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
            service.HttpHeaders.Add("Authorization", "Bearer " + tokenx);
            service.PreAuthenticate = true;
            service.SendClientLatencies = true;
            service.EnableScpLookup = false;
            service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

            ExFolder rootfolder = ExFolder.Bind(service, WellKnownFolderName.MsgFolderRoot);

        Console.WriteLine("The " + rootfolder.DisplayName + " has " + rootfolder.ChildFolderCount + " child folders.");

The important thing I noticed is I can't use the same token to access office365 api and EWS managed Api as EWS works with full mailbox access while office365 doesn't. 我注意到的重要一点是,我不能使用相同的令牌来访问office365 api和EWS托管的Api,因为EWS可以完全访问邮箱,而office365则不能。 I request the developer to confirm this,maybe I am doing something wrong, however my problem is solved for now. 我要求开发者确认这一点,也许我做错了,但是我的问题现在已经解决了。

Yep, that's right. 是的,是的。 The scope required for EWS isn't compatible with the Office 365 APIs, and vice versa. EWS所需的范围与Office 365 API不兼容,反之亦然。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM