简体   繁体   English

401尝试从EWS托管API发送电子邮件时未经授权

[英]401 Unauthorized when trying to send emails from EWS Managed API

Ok so I am developing an addin which must automatically send emails to others. 好的,所以我正在开发一个插件,该插件必须自动向他人发送电子邮件。 I know that office-js does not have permissions to do so and I have started using EWS Managed API. 我知道office-js无权这样做,并且我已经开始使用EWS托管API。 I have implemented SSO token and I get it with: 我已经实现了SSO令牌,并获得了:

Office.context.auth.getAccessTokenAsync()

After I get the token I make a request call to my server where I have my EWS Managed API and try to send an email with the folowing code: 获得令牌后,我将向拥有我的EWS托管API的服务器发出请求,并尝试发送包含以下代码的电子邮件:

ExchangeService exService = new ExchangeService();
exService.Url = new Uri(ewsUrl);
ExchangeCredentials credentials = new OAuthCredentials(ssoToken);
exService.Credentials = credentials;
EmailMessage emailMessage = new EmailMessage(exService);

When I call emailMessage.SendAndSaveCopy(); 当我致电emailMessage.SendAndSaveCopy(); it throws 401 Error. 它会引发401错误。

If I do use WebCredentials(user, pass) instead of OAuthCredentials(ssoToken) it does work. 如果我确实使用WebCredentials(user, pass)而不是OAuthCredentials(ssoToken)它确实可以工作。 I do not understand why (I also have set permissions on Azure Portal) 我不明白为什么(我在Azure Portal上也设置了权限)

Converged apps vs Old registrations 融合应用与旧注册

The SSO (Single Sign on) app that you had to create, needed to be created through https://apps.dev.microsoft.com . 您必须通过https://apps.dev.microsoft.com创建必须创建的SSO(单点登录)应用程序。 This was where you create the converged apps - which allows the consented parties to sign-in with their Outlook.com accounts or the organizational accounts (which is the newer way). 在此创建融合的应用程序-允许被许可方使用其Outlook.com帐户或组织帐户(较新的方式)登录。

However EWS is not using this. 但是EWS没有使用它。 EWS is using the authentication method of older way of creating apps, which is what you need to do through Azure Portal - through AAD App registrations. EWS使用的是较早的创建应用程序的身份验证方法,这是您需要通过Azure门户-通过AAD应用程序注册进行的操作。 More documentation on authentication of EWS here: https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/authentication-and-ews-in-exchange 有关EWS身份验证的更多文档,请访问: https : //docs.microsoft.com/zh-cn/exchange/client-developer/exchange-web-services/authentication-and-ews-in-exchange

So basically, the OAuth access token which you've retrieved can be used to query Microsoft Graph (as an example) but you can't use that to call EWS. 因此,基本上,您检索到的OAuth访问令牌可用于查询Microsoft Graph(作为示例),但是您不能使用它来调用EWS。

One very significant difference this creates is EWS is (as the name suggests, Exchange Web Services) is organizational accounts only. EWS产生的一个非常重要的区别是EWS(顾名思义,Exchange Web Services)仅是组织帐户。 You can't use that API for the Outlook.com users. 您不能为Outlook.com用户使用该API。

  • If you're using SSO framework, you are locked to the new way of app registrations. 如果您使用的是SSO框架,那么您将被锁定为的应用程序注册方式。 You can authenticate users from Outlook.com / or organizational accounts. 您可以从Outlook.com/或组织帐户对用户进行身份验证。 You can access Microsoft Graph and other similar sources - but not EWS. 您可以访问Microsoft Graph和其他类似资源-但不能访问EWS。
  • If you want to use EWS - you can't authenticate Outlook.com users. 如果要使用EWS,则无法对Outlook.com用户进行身份验证。 And you can't get the token which you can call EWS through SSO. 而且您无法获得可以通过SSO调用EWS的令牌。

Solutions 解决方案

  • With the access token that you retrieve (if you request the correct scopes) you can still send mail - and your solution would also work with Outlook.com users. 使用您检索的访问令牌(如果您请求正确的作用域),您仍然可以发送邮件-解决方案也可以与Outlook.com用户一起使用。 Check the Graph API: https://docs.microsoft.com/en-us/graph/api/resources/mail-api-overview?view=graph-rest-1.0 检查图谱API: https : //docs.microsoft.com/zh-cn/graph/api/resources/mail-api-overview?view= graph-rest- 1.0
  • If you insist on using EWS, you can drop the access token call and use the makeEwsRequestAsync method. 如果您坚持使用EWS,则可以删除访问令牌调用并使用makeEwsRequestAsync方法。 This will require you to write SOAP in javascript but it works. 这将要求您使用JavaScript编写SOAP,但是它可以工作。
  • If you dislike the idea of SOAP in JS (which I completely understand) and still want to use EWS through your C# code, you should drop using the SSO framework. 如果您不喜欢JS中的SOAP(我完全理解)的想法,但仍想通过C#代码使用EWS,则应该放弃使用SSO框架。 Instead authenticate the user through the Dialog API using OAuth against your AAD App registration (Not the converged model, but registration through the Azure Portal) 而是使用OAuth通过Dialog API通过Dialog API对用户进行身份验证,而不是对您的AAD App注册进行注册(不是融合模型,而是通过Azure门户进行注册)

您还必须将ExchangeServer.ImpersonatedUserId属性设置为代表您发送的用户的地址。

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

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