1

我目前正在尝试在 Salesforce 中调用亚马逊产品零售网络服务。

正如我在 Salesforce 中从 WSDL 生成 Apex 代码时遇到 WSDL 解析错误中提到的

我最初无法生成顶点存根类,但我按照@Ballinger 建议的方法创建了顶点类。我编写了一个顶点类来使用该存根并设置请求参数。我写的类如下

public class AmazonProductStubNew
{
 public static void getResults()
 { 
        System.Debug(' getResults start ');
        AmazonWS.AWSECommerceServicePortUS stub = new AmazonWS.AWSECommerceServicePortUS();

        stub.inputHttpHeaders_x = new Map<String,String>();
        stub.inputHttpHeaders_x.put('AWSAccessKeyId','MyAmazonAWSAccessKeyId');
        stub.inputHttpHeaders_x.put('Timestamp','2012-11-28T12:11:30Z');
        stub.inputHttpHeaders_x.put('Signature','Encrypted Secret Code');
        String MarketplaceDomain = '';
        String AWSAccessKeyId = 'MyAmazonAWSAccessKeyId';
        String AssociateTag = '';
        String XMLEscaping = '';
        String Validate = '';
        AmazonWS.ItemSearchRequest Shared = new AmazonWS.ItemSearchRequest();
        Shared.SearchIndex = 'DVD';
        AmazonWS.ItemSearchRequest[] Request = new AmazonWS.ItemSearchRequest[1];
        Request[0] = new AmazonWS.ItemSearchRequest();
        Request[0].Title = 'Inception';
        AmazonWS.ItemSearchResponse_element response = stub.ItemSearch(MarketplaceDomain,AWSAccessKeyId,AssociateTag,XMLEscaping,Validate,Shared,Request);
        AmazonWS.Items_element[] localItems = response.Items;
        System.Debug(localItems[0].TotalResults);
 }
 }

即使我已将 HTTP 标头添加到存根,我也没有在 XML 请求消息中得到它 XML 请求如下

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header />
<env:Body>
<ItemSearch xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<MarketplaceDomain>
</MarketplaceDomain>
<AWSAccessKeyId>MyAWSAccessKeyId</AWSAccessKeyId>
<AssociateTag></AssociateTag>
<XMLEscaping></XMLEscaping>
<Validate></Validate>
<Shared><SearchIndex>DVD</SearchIndex></Shared>
<Request><Title>Inception</Title>
</Request></ItemSearch>
</env:Body></env:Envelope>

由于 SOAP 请求中没有标头,因此存在 SOAP 错误,要求来自 Amazon Server 的签名。

如您所见,我是 Salesforce Apex 的新手。我按照中的步骤

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_web_services_wsdl2apex.htm#http_header_support

设置标题。

关于为什么没有添加标题的任何想法?

PS 我手动添加了标题并在 SOAP UI 中进行了尝试,我得到了正确的响应。

谢谢 :)

4

1 回答 1

1

我认为您使用了错误的功能:)(问题确实令人困惑)。

SOAP(或通常是 HTTP)通信包括发送标头和实际消息(如果您愿意,可以使用有效负载)。标头是短文本,消息通常是巨大的 XML。

您的代码正在设置HTTP 标头(用于 Web 通信以进行身份​​验证、提供有关您的浏览器的信息、首选语言、设置 cookie、返回状态代码(如 404 页面未找到...)...)请不要对“for傻瓜”,但我意识到维基百科的文章有点太多了,这可能更简单: http: //net.tutsplus.com/tutorials/other/http-headers-for-dummies/

我怀疑亚马逊的网络服务想要的只是<env:Header>...</env:Header>标签内的一些字段?只需检查生成的顶点代码是否存在名为“Header”的子类(您也可以搜索“Signature”之类的变量名称。这将是一个完全疯狂的猜测,但我认为您必须编写类似的内容:

AmazonWS.AWSECommerceServicePortUS stub = new AmazonWS.AWSECommerceServicePortUS();
AmazonWS.Header h = new AmazonWS.Header();
h.AWSAccessKeyId = 'MyAmazonAWSAccessKeyId';
h.Timestamp = '2012-11-28T12:11:30Z';
h.Signature = 'Encrypted Secret Code';
stub.Header = h; // plug it into the request

// create and plug other required tags
AmazonWS.ItemSearchRequest Shared = new AmazonWS.ItemSearchRequest();
Shared.SearchIndex = 'DVD';
AmazonWS.ItemSearchRequest[] Request = new AmazonWS.ItemSearchRequest[1];
Request[0] = new AmazonWS.ItemSearchRequest();
Request[0].Title = 'Inception';
// ...

现在,为了使它更加混乱,您可能仍然必须使用 HTTP 标头,有一个特殊的标头,称为SOAPAction。但一般来说,我相信您将数据放在 XML 中,而不是放在 http 标头中。


有趣的是,我已经从http://aws.amazon.com/code/Product-Advertising-API/2478下载了 Java 示例,如果我阅读正确,他们会在 URL(端点)中传递签名,而不是在 XML 中。可能是因为它是一种 REST GET 方法(如果您可以访问该 API,它可以为您节省很多头发,SOAP 很笨重)。

于 2012-11-28T19:28:06.133 回答