EWS SOAP请求失败

时间:2016-04-21 19:34:11

标签: xml soap wsdl exchangewebservices soaplite

我正在使用PERL和SOAP :: Lite来对MS Exchange Web服务进行soap调用。我已经想出了身份验证并使用Oauth令牌进行调用。我试图调用记录为here的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“。预期的可能元素列表:名称空间”http://schemas.microsoft.com/exchange/services/2006/messages“中的”MailboxSmtpAddress“ “

<?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>

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

指定了MailboxSmtpAddress元素的名称空间

产生:

<?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,因此非常感谢任何建议。

1 个答案:

答案 0 :(得分: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> 

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