简体   繁体   English

亚马逊产品广告API ItemSearch返回空值

[英]Amazon Product Advertising API ItemSearch Returning Null

I have spent entire day trying to fix problems with the Amazon ECS(Ecommerce Services) API. 我花了一整天的时间来解决Amazon ECS(电子商务服务)API的问题。

I have downloaded the example on their site for sending SOAP Requests with .NET 4.0 and WCF 我已经在他们的网站上下载了该示例,以使用.NET 4.0和WCF发送SOAP请求

http://aws.amazon.com/code/Product-Advertising-API/3941 http://aws.amazon.com/code/Product-Advertising-API/3941

I have not changed anything in the code from the example, except for the AccessKeyID and SecretyKeyID in the config file. 除了配置文件中的AccessKeyID和SecretyKeyID外,我没有更改示例中的代码。

The Calling code looks like the following: 调用代码如下所示:

        // Instantiate Amazon ProductAdvertisingAPI client
        AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();

        // prepare an ItemSearch request
        ItemSearchRequest request = new ItemSearchRequest();
        request.SearchIndex = "Books";
        request.Title = "WCF";
        request.ResponseGroup = new string[] { "Medium"};

        ItemSearch itemSearch = new ItemSearch();
        itemSearch.Request = new ItemSearchRequest[] { request };
        request.Condition = Condition.All;
        itemSearch.AssociateTag = "";
        itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];

        // send the ItemSearch request
        ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);
        if (response != null)
        {
            // write out the results from the ItemSearch request
            foreach (var item in response.Items[0].Item)
            {
                Console.WriteLine(item.ItemAttributes.Title);
            }
        }
        Console.WriteLine("done...enter any key to continue>");
        Console.ReadLine();

the Call to the ItemSearch() is returning a null object. 对ItemSearch()的调用返回一个空对象。 I looked further into this and saw that in the AmazongSigningMessageInspector Class, the AfterReceiveReply() method is showing that a proper SOAP XML response is being returned with results, so I know that it is making the call to the service and returning correctly. 我对此进行了进一步研究,发现在AmazongSigningMessageInspector类中,AfterReceiveReply()方法显示正在返回带有结果的正确SOAP XML响应,因此我知道它正在对服务进行调用并正确返回。 For some reason though I am left with a NULL ItemSearch object. 由于某种原因,尽管我留下了一个NULL ItemSearch对象。

The code for my classes are the following: 我的类的代码如下:

class AmazonSigningBehaviorExtensionElement : BehaviorExtensionElement
{
    public AmazonSigningBehaviorExtensionElement()
    {
    }

    public override Type BehaviorType
    {
        get
        {
            return typeof(AmazonSigningEndpointBehavior);
        }
    }

    protected override object CreateBehavior()
    {
        return new AmazonSigningEndpointBehavior(AccessKeyId, SecretKey);
    }

    [ConfigurationProperty("accessKeyId", IsRequired = true)]
    public string AccessKeyId
    {
        get { return (string)base["accessKeyId"]; }
        set { base["accessKeyId"] = value; }
    }

    [ConfigurationProperty("secretKey", IsRequired = true)]
    public string SecretKey
    {
        get { return (string)base["secretKey"]; }
        set { base["secretKey"] = value; }
    }
}


public class AmazonSigningEndpointBehavior : IEndpointBehavior {
    private string  _accessKeyId    = "";
    private string  _secretKey  = "";

    public AmazonSigningEndpointBehavior()
    {
        this._accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
        this._secretKey = ConfigurationManager.AppSettings["secretKey"];
    }

    public AmazonSigningEndpointBehavior(string accessKeyId, string secretKey) {
        this._accessKeyId   = accessKeyId;
        this._secretKey     = secretKey;
    }

    public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) {
        clientRuntime.MessageInspectors.Add(new AmazonSigningMessageInspector(_accessKeyId, _secretKey));
    }

    public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher) { return; }
    public void Validate(ServiceEndpoint serviceEndpoint) { return; }
    public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters) { return; }
}

public class AmazonSigningMessageInspector : IClientMessageInspector {
    private string  _accessKeyId    = "";
    private string  _secretKey  = "";

    public AmazonSigningMessageInspector(string accessKeyId, string secretKey) {
        this._accessKeyId   = accessKeyId;
        this._secretKey     = secretKey;
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel) {
        // prepare the data to sign
        string      operation       = Regex.Match(request.Headers.Action, "[^/]+$").ToString();
        DateTime    now             = DateTime.UtcNow;
        string      timestamp       = now.ToString("yyyy-MM-ddTHH:mm:ssZ");
        string      signMe          = operation + timestamp;
        byte[]      bytesToSign     = Encoding.UTF8.GetBytes(signMe);

        // sign the data
        byte[]      secretKeyBytes  = Encoding.UTF8.GetBytes(_secretKey);
        HMAC        hmacSha256      = new HMACSHA256(secretKeyBytes);
        byte[]      hashBytes       = hmacSha256.ComputeHash(bytesToSign);
        string      signature       = Convert.ToBase64String(hashBytes);

        // add the signature information to the request headers
        request.Headers.Add(new AmazonHeader("AWSAccessKeyId", _accessKeyId));
        request.Headers.Add(new AmazonHeader("Timestamp", timestamp));
        request.Headers.Add(new AmazonHeader("Signature", signature));

        return null;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState)
    {


    }
}

I have seen this problem everywhere, but nobody has posted a fix for it anywhere. 我到处都看到了这个问题,但是没有人在任何地方发布过修复程序。 Somebody please help me with this. 有人请帮我。

My issue was that I was missing the associate tag. 我的问题是我缺少关联标签。

itemSearch.AssociateTag = "213";

There are definitely problems with the generated code, ItemSearchResponse contains an error collection which is not exposed by code. 生成的代码肯定存在问题,ItemSearchResponse包含一个错误集合,该错误集合未由代码公开。 It was through looking at the raw message in the inspector that pointed me in the right direction. 正是通过查看检查员中的原始消息,才向我指明了正确的方向。

The answer regarding missing associate tag help me, but I also had to ensure the WSDL URL and endpoint address match the Amazon site you've registered under. 关于缺少关联标签的答案对我有帮助,但我还必须确保WSDL URL和端点地址与您在其下注册的Amazon网站匹配。 I registered under the UK site so I need to use. 我在英国网站上注册,所以我需要使用。

WSDL WSDL
Endpoint Address 端点地址

A current project to this assignment you can found here on Nager.AmazonProductAdvertising github. 您可以在Nager.AmazonProductAdvertising github上找到此任务的最新项目。 The library is also available over nuget 该库也可以通过nuget使用

nuget 的NuGet

PM> Install-Package Nager.AmazonProductAdvertising

Example

var authentication = new AmazonAuthentication();
authentication.AccessKey = "accesskey";
authentication.SecretKey = "secretkey";

var wrapper = new AmazonWrapper(authentication, AmazonEndpoint.US);
var result = wrapper.Search("canon eos", AmazonSearchIndex.Electronics, AmazonResponseGroup.Large);

I compiled and posted a fix for this sample. 我编译并发布了此示例的修复程序。 Please go here: https://forums.aws.amazon.com/message.jspa?messageID=440527#440527 请转到此处: https : //forums.aws.amazon.com/message.jspa?messageID=440527#440527

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM