Microsoft EWS api GetItem(邮件)响应 ErrorInvalidUserPrincipalName 但主体名称正确

Microsoft EWS api GetItem (mail) response ErrorInvalidUserPrincipalName but principal name is correct

我使用 Microsoft EWS api GetItem 来获取邮件消息项目,但是对于 某些用户 ,服务器响应 ErrorInvalidUserPrincipalName(其他用户工作无误),我用 Microsoft Graph 检查主体名称是否正确 api。

<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" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
    <soap:Header>
        <t:RequestServerVersion Version="Exchange2013"/>
        <t:ExchangeImpersonation>
            <t:ConnectingSID>
                <t:PrincipalName>xxx@xxx.com</t:PrincipalName>
            </t:ConnectingSID>
        </t:ExchangeImpersonation>
    </soap:Header>
    <soap:Body>
        <m:GetItem>
            <m:ItemShape>
                <t:BaseShape>IdOnly</t:BaseShape>
                <t:IncludeMimeContent>true</t:IncludeMimeContent>
            </m:ItemShape>
            <m:ItemIds>
                <t:ItemId Id="xxx"/>
            </m:ItemIds>
        </m:GetItem>
    </soap:Body>
</soap:Envelope>

并且服务器响应ErrorInvalidUserPrincipalName

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://schemas.microsoft.com/exchange/services/2006/types">a:ErrorInvalidUserPrincipalName</faultcode>
            <faultstring xml:lang="en-US">The impersonation principal name is invalid.</faultstring>
            <detail>
                <e:ResponseCode xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">ErrorInvalidUserPrincipalName</e:ResponseCode>
                <e:Message xmlns:e="http://schemas.microsoft.com/exchange/services/2006/errors">The impersonation principal name is invalid.</e:Message>
            </detail>
        </s:Fault>
    </s:Body>
</s:Envelope>

我使用 Microsoft Graph 用户 api 检查主体名称,主体名称与我在 EWS GetItem 请求中引入的名称相同,但仍然得到 ErrorInvalidUserPrincipalName 响应。

 {
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id": "zzz",
"businessPhones": [],
"displayName": "yyy",
"mail": "xxx@xxx.com",
"userPrincipalName": "xxx@xxx.com"
...
}

我尝试使用 PrimarySmtpAddress 标签而不是 PrincipalName 发送相同的 EWS GetItem(smtp 地址与主体名称相同),它工作没有错误,我不知道为什么使用 PrincipalName 会得到 ErrorInvalidUserPrincipalName 响应,即使主体名称看起来正确。

<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" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">
    <soap:Header>
        <t:RequestServerVersion Version="Exchange2013"/>
        <t:ExchangeImpersonation>
            <t:ConnectingSID>
               <t:PrimarySmtpAddress>xxx@xxx.com</t:PrimarySmtpAddress>
            </t:ConnectingSID>
        </t:ExchangeImpersonation>
    </soap:Header>
    <soap:Body>
        <m:GetItem>
            <m:ItemShape>
                <t:BaseShape>IdOnly</t:BaseShape>
                <t:IncludeMimeContent>true</t:IncludeMimeContent>
            </m:ItemShape>
            <m:ItemIds>
                <t:ItemId Id="xxx"/>
            </m:ItemIds>
        </m:GetItem>
    </soap:Body>
</soap:Envelope>

有没有人遇到同样的问题可以帮我解决这个问题?

谢谢!

邮箱是否具有有效许可证(EXCHANGEDESKLESS Exchange Online Kiosk 许可证也没有 EWS 访问权限)并且是否已启用并且至少登录过一次。通常,当您收到此错误时,是因为您试图冒充已禁用的帐户。

使用PowerShell检查后,发现UserPrincipalName在Exchange Server上不一致。

Get-Mailbox | Select-Object -ExpandProperty UserPrincipalName

Output: aaa@aaa.com

但是图表 api return UserPrincipalName 是 xxx@xxx.com

然后联系Microsoft支持,他们帮助设置UserPrincipalName一致,UserPrincipalName一致后,EWS api GetItem可以与PrincipalName完美配合。

谢谢。