Exchange (EWS SOAP) 获取带有模拟返回 500 的文件夹 - 请求无效

Exchange (EWS SOAP) Get Folder with Impersonation return 500 - Invalid Request

提问人:Vince 提问时间:4/26/2023 更新时间:6/7/2023 访问量:95

问:

我正在尝试使用 SOAP 调用 EWS 并将旧的旧版身份验证代码替换为 OAuth。 但我总是收到“500 - 请求无效”。

我能够使用 c# 和 nuget“Microsoft.Identity.Client”和“Microsoft.Exchange.WebServices”获取令牌。 它可以列出我邮箱中的文件夹,我可以在这些文件夹中获取电子邮件。

enter image description here

但是我不能使用 C# 应用程序,因为我们的应用程序位于 Access (VBA) 中,并且代码与 SOAP 的逻辑耦合。像 5-10 年前一样制作应用程序的人不记得应用程序的逻辑,现在需要数据......

我可以使用的是这个小概念证明中的访问令牌。 因此,我尝试使用从 Microsoft.Identity.Client 获得的访问令牌并将其添加到 VBA 调用中,它似乎有效,因为我没有得到 401/403。 最初我有 401,因为我对 Azure 没有正确的权限,但现在似乎没问题。

与 c# 代码一样,我已将 Impersonation 添加到 SOAP 请求中,以保持与 c# 代码相同。

下面是 SOAP 请求

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
>
  <soap:Header>
    <t:ExchangeImpersonation>
      <t:ConnectingSID>
        <t:PrimarySmtpAddress>
          [email protected]
        </t:PrimarySmtpAddress>
      </t:ConnectingSID>
    </t:ExchangeImpersonation>
  </soap:Header>
  <soap:Body>
    <GetFolder xmlns="https://schemas.microsoft.com/exchange/services/2006/messages"
               xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
      <FolderShape>
        <t:BaseShape>Default</t:BaseShape>
      </FolderShape>
      <FolderIds>
        <t:DistinguishedFolderId Id="inbox"/>
      </FolderIds>
    </GetFolder>
  </soap:Body>
</soap:Envelope>

该服务不是很冗长,所以我不知道请求中有什么问题!

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <Action xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none" s:mustUnderstand="1">*</Action>
   </s:Header>
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorInvalidRequest</faultcode>
         <faultstring xml:lang="en-US">The request is invalid.</faultstring>
         <detail>
            <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorInvalidRequest</e:ResponseCode>
            <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The request is invalid.</e:Message>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

为了进行测试,我在这里为 GetFolder 服务提供了 XML 的主要部分

https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/getfolder-operation

我已经找到了要在此处添加的标题

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-identify-the-account-to-impersonate

与 c# 请求相比,此请求有什么问题?

他们使用相同的访问令牌,尝试执行相同的操作。 但是 SOAP 调用失败了。

C# VBA MS-ACCESS OAUTH-2.0 ExchangeWebServices

评论

0赞 Tim Williams 4/26/2023
也许看看这个:命名空间声明 stackoverflow.com/a/68706433/478884 https/http 问题。
0赞 Vince 4/27/2023
感谢您的支持,您帮助我开始了解问题的真正所在

答:

2赞 Glen Scales 4/27/2023 #1

您的架构不正确,如果您已从 Microsoft 文档中提取示例,那么这就是问题所在。例如

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"
>

    <GetFolder xmlns="https://schemas.microsoft.com/exchange/services/2006/messages"
           xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">

应该是

<soap:Envelope 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
>

    <GetFolder xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
           xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">

EWS 中的 XML 架构始终是 http 而不是 https(请注意,这只是一个架构声明,它与正在使用的实际协议无关),Microsoft 通过错误地更新其文档中的架构值,在其 Exchange 文档中产生了问题。我已经修复了其中的一些,因为我发现了它们,但有很多问题。

评论

0赞 Vince 4/27/2023
谢谢,我正在浏览器中测试 url 和 HTTP 重定向到 HTTPS,HTTPS 说它已被移动,所以我有点迷茫;)非常感谢,它现在可以正常工作。
0赞 Alexey 6/7/2023 #2

因为它是 Office 365,所以设置 O365 守护程序/应用注册,然后使用 REST API 网关(如 Aurinko)可能更容易。