【问题标题】:Bing Translation Api access via SOAP interface from Ruby通过 Ruby 的 SOAP 接口访问 Bing Translation Api
【发布时间】:2013-10-07 13:26:37
【问题描述】:

我正在尝试使用 Bing Translator SOAP API(由于在 HTTP API 中,由于 UTF-8 序列化,对于不太大的请求,我收到 414“请求太长”)。

所以,我正在使用 bing_translator gem source 尝试使用 Savon SOAP 工具包将其从 HTTP 接口切换到 SOAP 接口。

我的工作流程如下(access token getting function 未显示):

WSDL_URI = 'http://api.microsofttranslator.com/V2/soap.svc?wsdl'

get_access_token
client = Savon.client(wsdl: WSDL_URI, headers: {'Authorization' => "Bearer #{@access_token['access_token']}"})

params = {
  'from'        => 'ru',
  'to'          => 'en',
  'text'        => 'Это текст для перевода',
  'category'    => 'general',
  'contentType' => 'text/plain'
}

result = client.call(:translate, message: params)

然后 SOAP 请求执行:

SOAP request: http://api.microsofttranslator.com/V2/soap.svc

Authorization: Bearer http%3a%2f%2fschemas.xmlsoap.org%2fws%2f2005%2f05%2fidentity%2fclaims%2fnameidentifier=invest_amurobl_ru&http%3a%2f%2fschemas.microsoft.com%2faccesscontrolservice%2f2010%2f07%2fclaims%2fidentityprovider=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&Audience=http%3a%2f%2fapi.microsofttranslator.com&ExpiresOn=1381128612&Issuer=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&HMACSHA256=Mw41PMMgw2n6ZVaGRXtwfR0vwMJUyIMltIyd9pa9MqA%3d
SOAPAction: "http://api.microsofttranslator.com/V2/LanguageService/Translate"
Content-Type: text/xml;charset=UTF-8
Content-Length: 454

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="http://tempuri.org/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<wsdl:Translate>
<to>en</to>
<text>Это текст для перевода</text>
<category>general</category>
<contentType>text/html</contentType>
<from>ru</from>
</wsdl:Translate>
</env:Body>
</env:Envelope>

我收到错误 500:Unhandled Service Exception

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-US">Unhandled Service Exception</faultstring>
<detail>
<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1</int>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

可能有什么问题?任何已经使用 Bing Translator SOAP API 来区分我的肥皂消息的人都可以吗?有关如何解决此问题的任何建议。

感谢关注。

编辑:

按照@SteffenRoller 的建议,我已经使用 SoapUI 检查了 API,它可以正常工作。这是 SoapUI 生成的 XML(值是手动插入的):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v2="http://api.microsofttranslator.com/V2">
   <soapenv:Header/>
   <soapenv:Body>
      <v2:Translate>
         <!--Optional:-->
         <v2:text>Текст, который я хочу перевести</v2:text>
         <!--Optional:-->
         <v2:from>ru</v2:from>
         <!--Optional:-->
         <v2:to>en</v2:to>
         <!--Optional:-->
         <v2:contentType>text/plain</v2:contentType>
         <!--Optional:-->
         <v2:category>general</v2:category>
      </v2:Translate>
   </soapenv:Body>
</soapenv:Envelope>

如您所见,唯一的区别是&lt;Body&gt; 中的所有标签都在v2 命名空间中。在由 Savon 生成的 XML 中,这个命名空间根本不存在。

那么,现在的问题是:如何指示 Savon 为消息正文中的标签使用正确的命名空间?

虽然我认为这是一个 Savon 错误,但我会将其提交给开发人员,因为 SoapUI 已经通过相同的 WSDL 生成了正确的 XML,而 Savon 没有。

【问题讨论】:

  • 你试过SoapUI吗?它奏效了吗?首先让它与 SoapUI 一起工作。在 Ruby/Savon 的第二步中实现它。
  • @SteffenRoller,感谢您的建议。它适用于 SoapUI,但 XML 不同,请参阅更新后的问题。

标签: ruby web-services soap bing-api savon


【解决方案1】:

好的。这是一个已知错误:Savon issue #340。它与复合 WSDL 文件相关,不会在当前主要版本中修复:-(

因此,在我们的例子中,我们需要告诉 Savon,我们的命名空间是什么,它的名称是什么(只是为了使其与 WSDL 保持一致)并在每个标签的名称前面加上这个命名空间。

正确的代码如下所示:

require 'savon'

access_token = "http%3a%2f%2fschemas.xmlsoap.org%2fws...CA9TEs%3d"

client = Savon.client(
  wsdl: 'http://api.microsofttranslator.com/V2/soap.svc?wsdl',
  namespace: 'http://api.microsofttranslator.com/V2',
  namespace_identifier: :v2,
  headers: {'Authorization' => "Bearer #{access_token}"},
)

params = {
  'v2:text'        => 'Это текст для перевода',
  'v2:from'        => 'ru',
  'v2:to'          => 'en',
  'v2:contentType' => 'text/plain',
  'v2:category'    => 'general',
}

result = client.call(:translate, message: params)

puts result.body[:translate_response][:translate_result]

欢迎任何建议和更正。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2015-08-17
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多