0

我正在使用 PERL 和 SOAP::Lite 对 MS Exchange Web 服务进行肥皂调用。我已经弄清楚了身份验证并正在使用 Oauth 令牌进行调用。我正在尝试调用此处记录的 GetInboxRules 。

基本上电话需要看起来像这样。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
        xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010_SP1" />
  </soap:Header>
  <soap:Body>
    <m:GetInboxRules>
      <m:MailboxSmtpAddress>User1@Contoso.com</m:MailboxSmtpAddress>
    </m:GetInboxRules>
  </soap:Body>
</soap:Envelope>

我的第一次尝试使用了以下代码:

my $client = SOAP::Lite->service('file://home/auth/work/src/ben/services.wsdl')->proxy('https://outlook.office365.com/EWS/Exchange.asmx');
$client->readable(1)->autotype(0)->outputxml('true');
$client->ns('http://schemas.microsoft.com/exchange/services/2006/messages', 'm');

my $ua = $client->schema->useragent;
$ua->default_header('Authorization' => $auth_header);
$ua->default_header('Content-Type' => 'application/xml');
$client->schema->useragent($ua);

$client->transport->http_request->headers->push_header('Authorization'=> $auth_header);

# WITH URI
my $som = $client->call('GetInboxRules', SOAP::Data->name('MailboxSmtpAddress')->value('eg7636@foo.com')->uri('http://schemas.microsoft.com/exchange/services/2006/messages'));

这产生了以下 XML 以及 500 内部服务器错误说明:

“请求未通过架构验证:命名空间“ http://schemas.microsoft.com/exchange/services/2006/messages ”中的元素“GetInboxRules”具有无效的子元素“MailboxSmtpAddress”。预期的可能元素列表:“MailboxSmtpAddress”在命名空间' http://schemas.microsoft.com/exchange/services/2006/messages '"

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <m:GetInboxRules>
      <MailboxSmtpAddress>eg7636@foo.com</MailboxSmtpAddress>
    </m:GetInboxRules>
  </soap:Body>
</soap:Envelope>

我已经在设置名称空间,但为了尝试解决这个问题,我通过向 MailboxSmtpAddress 元素添加 uri 规范来指定名称空间my $som = $client->call('GetInboxRules', SOAP::Data->name('MailboxSmtpAddress')->value('eg7636@foo.com')->uri('http://schemas.microsoft.com/exchange/services/2006/messages'));

这产生了:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:namesp3="http://schemas.microsoft.com/exchange/services/2006/messages"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <m:GetInboxRules>
      <namesp3:MailboxSmtpAddress xmlns:namesp3="http://schemas.microsoft.com/exchange/services/2006/messages">eg7636@foo.com</namesp3:MailboxSmtpAddress>
    </m:GetInboxRules>
  </soap:Body>
</soap:Envelope>

然后是 400 Bad Request 响应。我猜测错误的请求响应是因为 MailboxSmtpAddress 标记中包含 url,但我不知道如何指定命名空间。

这是我第一次使用 SOAP,因此非常感谢任何建议。

4

1 回答 1

0

一位圣人来救我,并给了我以下代码:

my $client = SOAP::Lite
    ->readable(1)
    ->proxy('https://outlook.office365.com/EWS/Exchange.asmx')
    ->ns( 'http://schemas.microsoft.com/exchange/services/2006/types', 'typ')
    ->ns( 'http://schemas.microsoft.com/exchange/services/2006/messages', 'mes')
    ->envprefix('soapenv')
    ->autotype(0);

$client->serializer->{'_attr'}{'{http://schemas.xmlsoap.org/soap/envelope/}encodingStyle'} = undef;


$client->transport->default_header('Authorization' => $auth_header);
$client->transport->default_header('X-AnchorMailbox' => 'eg7636@foo.onmicrosoft.com');
$client->transport->default_header('X-OWA-ExplicitLogonUser' => 'eg7636@foo.onmicrosoft.com');

my @header = (
    SOAP::Header->name('typ:RequestServerVersion')->attr({'Version' => 'Exchange2015'}),
    SOAP::Header->name('typ:ExchangeImpersonation')->value(\SOAP::Header->name('typ:ConnectingSID')
        ->value(\SOAP::Header->name('typ:SmtpAddress')->value('eg7636@foo.com')))
);

my @param;
my $method;

@param = (
     SOAP::Data->name('mes:MailboxSmtpAddress', 'eg7636@foo.com'),
     );

$method = SOAP::Data->name('mes:GetInboxRules');

my $response = $client->call($method => (@param, @header));

print Dumper($response);

这导致命名空间标记被放置在传递给方法调用的元素上,并且还纠正了我在标题格式方面遇到的一些问题。

以下是来自该代码的 XML:

<soapenv:Envelope
    xmlns:mes="http://schemas.microsoft.com/exchange/services/2006/messages"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:typ="http://schemas.microsoft.com/exchange/services/2006/types"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <typ:RequestServerVersion Version="Exchange2015" />

    <typ:ExchangeImpersonation>
      <typ:ConnectingSID>
        <typ:SmtpAddress>eg7636@foo.com</typ:SmtpAddress>
      </typ:ConnectingSID>
    </typ:ExchangeImpersonation>
  </soapenv:Header>

  <soapenv:Body>
    <mes:GetInboxRules>
      <mes:MailboxSmtpAddress>eg7636@foo.com</mes:MailboxSmtpAddress>
    </mes:GetInboxRules>
  </soapenv:Body>
</soapenv:Envelope> 

这引起了微软的成功回应。

于 2016-04-25T14:09:32.113 回答