oauth-2.0 错误:此类型的oauth令牌必须存在ExchangeImpersonation SOAP标头

cvxl0en2  于 2022-10-31  发布在  其他
关注(0)|答案(2)|浏览(186)

我正在尝试将Exchange Web服务(EWS)与应用程序和OAuth2身份验证一起使用。
我做了什么:

  • 在Azure门户应用程序上注册,授予所有必需的权限(甚至Exchange的full_access_as_app);以管理员身份接受了这些权限;
  • 准备了正确的OAuth2承载令牌(范围为https://outlook.office365.com/.default);
  • 使用SOAP UI准备好SOAP请求。作为此请求的基础,我使用以下请求:https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/getattachment-operation。此外,我还添加了带有承载令牌的HTTP授权标头、带有目标邮箱的X-AnchorMailbox。

结果我得到这一条:“对于此类型的OAuth令牌,必须存在ExchangeImpersonation SOAP标头”。我不知道应如何修复该问题。
甚至尝试使用ExchangeImpersonate和SmtpAddress与上面相同的邮箱地址,但以这种方式,我得到另一个错误“邮箱未找到”。但邮箱是!(在几个邮箱上测试)。
我的疑问是:我在使用EWS时做错了什么?
请求:

POST https://outlook.office365.com/ews/Exchange.asmx
Authorization: Bearer <token>
X-AnchorMailbox: mailbox@something.onmicrosoft.com

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
  <soap:Header>
        <t:RequestServerVersion Version="Exchange2016"/>
        <t:ExchangeImpersonation>
            <t:ConnectingSID>
                <t:PrimarySmtpAddress>
                    mailbox@something.onmicrosoft.com
                </t:PrimarySmtpAddress>
            </t:ConnectingSID>
        </t:ExchangeImpersonation>
  </soap:Header>
  <soap:Body>
    <GetAttachment xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <AttachmentShape/>
      <AttachmentIds>
        <t:AttachmentId Id="AAMkADAwNzdjNTg3LTc4M2ItNDE0Yi05MTk4LTQxZDBlYTc1NmMxZgBGAAAAAAAiixtLGariQY7rf5pAKRZZBwBVk8babsuEQ4s2Znfj9fB5AAAAAAEMAABVk8babsuEQ4s2Znfj9fB5AADq3B14AAABEgAQAKZOAcjZCBxHpqvY6XmXp5w="/>
      </AttachmentIds>
    </GetAttachment>
  </soap:Body>
</soap:Envelope>

回应:

ErrorNonExistentMailbox The SMTP address has no mailbox associated with it.
vsikbqxv

vsikbqxv1#

这个帮我修好了。

_exchangeService.ImpersonatedUserId = 
    new ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmailAddress);
yizd12fk

yizd12fk2#

您需要执行严修化仿真,如下所示:

var ews = new Rebex.net.Ews();
ews.Connect(outlook.office365.com);
ews.Settings.Impersonation = new EwsImpersonation();
ews.Settings.Impersonation.SmtpAddress = <mailbox address>;

相关问题