簡體   English   中英

亞馬遜產品廣告API ItemSearch返回空值

[英]Amazon Product Advertising API ItemSearch Returning Null

我花了一整天的時間來解決Amazon ECS(電子商務服務)API的問題。

我已經在他們的網站上下載了該示例,以使用.NET 4.0和WCF發送SOAP請求

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

除了配置文件中的AccessKeyID和SecretyKeyID外,我沒有更改示例中的代碼。

調用代碼如下所示:

        // 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();

對ItemSearch()的調用返回一個空對象。 我對此進行了進一步研究,發現在AmazongSigningMessageInspector類中,AfterReceiveReply()方法顯示正在返回帶有結果的正確SOAP XML響應,因此我知道它正在對服務進行調用並正確返回。 由於某種原因,盡管我留下了一個NULL ItemSearch對象。

我的類的代碼如下:

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)
    {


    }
}

我到處都看到了這個問題,但是沒有人在任何地方發布過修復程序。 有人請幫我。

我的問題是我缺少關聯標簽。

itemSearch.AssociateTag = "213";

生成的代碼肯定存在問題,ItemSearchResponse包含一個錯誤集合,該錯誤集合未由代碼公開。 正是通過查看檢查員中的原始消息,才向我指明了正確的方向。

關於缺少關聯標簽的答案對我有幫助,但我還必須確保WSDL URL和端點地址與您在其下注冊的Amazon網站匹配。 我在英國網站上注冊,所以我需要使用。

WSDL
端點地址

您可以在Nager.AmazonProductAdvertising github上找到此任務的最新項目。 該庫也可以通過nuget使用

的NuGet

PM> Install-Package Nager.AmazonProductAdvertising

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);

我編譯並發布了此示例的修復程序。 請轉到此處: https : //forums.aws.amazon.com/message.jspa?messageID=440527#440527

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM