asp.net amazon itemsearch

时间:2011-03-17 14:26:42

标签: asp.net amazon

我正在使用visual studio 2008并尝试像书籍或其他东西一样进行API项目搜索。 我只使用accessKeyId,但是当我点击我的“搜索”按钮来搜索书籍时,就会出现签名错误。 我还需要使用secretKeyID吗? 我刚刚在visual studio中创建了新的ASP.Net网站或者我必须使用AWS SDK for.Net包吗?

请有人给个好建议。 谢谢!

1 个答案:

答案 0 :(得分:2)

签名错误意味着您没有正确创建请求签名。您应该使用SprightlySoft AWS Component for .NET。它是免费的,它支持产品广告API。在http://sprightlysoft.com/获取。 AWS SDK for .NET不适用于Product Advertising API。

以下是使用SprightlySoft组件调用ItemSearch的示例。

//Product Advertising API, ItemSearch: http://docs.amazonwebservices.com/AWSECommerceService/2010-10-01/DG/ItemSearch.html

SprightlySoftAWS.REST MyREST = new SprightlySoftAWS.REST();

String RequestURL;
RequestURL = "https://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&Operation=ItemSearch&Version=2010-10-01";
RequestURL += "&AWSAccessKeyId=" + System.Uri.EscapeDataString(TextBoxAWSAccessKeyId.Text) + "&SignatureVersion=2&SignatureMethod=HmacSHA256&Timestamp=" + Uri.EscapeDataString(DateTime.UtcNow.ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z"));
RequestURL += "&Actor=" + System.Uri.EscapeDataString("Tom Cruise");
RequestURL += "&SearchIndex=DVD";
RequestURL += "&ResponseGroup=" + System.Uri.EscapeDataString("ItemAttributes,Reviews");
RequestURL += "&Sort=salesrank";
RequestURL += "&ItemPage=1";

String RequestMethod;
RequestMethod = "GET";

String SignatureValue;
SignatureValue = MyREST.GetSignatureVersion2Value(RequestURL, RequestMethod, "", TextBoxAWSSecretAccessKey.Text);

RequestURL += "&Signature=" + System.Uri.EscapeDataString(SignatureValue);

Boolean RetBool;
RetBool = MyREST.MakeRequest(RequestURL, RequestMethod, null);
System.Diagnostics.Debug.Print(MyREST.LogData);

String ResponseMessage = "";

if (RetBool == true)
{
    System.Xml.XmlDocument MyXmlDocument;
    System.Xml.XmlNamespaceManager MyXmlNamespaceManager;
    System.Xml.XmlNode MyXmlNode;
    System.Xml.XmlNodeList MyXmlNodeList;

    MyXmlDocument = new System.Xml.XmlDocument();
    MyXmlDocument.LoadXml(MyREST.ResponseString);

    MyXmlNamespaceManager = new System.Xml.XmlNamespaceManager(MyXmlDocument.NameTable);
    MyXmlNamespaceManager.AddNamespace("amz", "http://webservices.amazon.com/AWSECommerceService/2010-10-01");

    MyXmlNodeList = MyXmlDocument.SelectNodes("amz:ItemSearchResponse/amz:Items/amz:Item", MyXmlNamespaceManager);

    if (MyXmlNodeList.Count == 0)
    {
        ResponseMessage = "No items exist.";
    }
    else
    {
        foreach (System.Xml.XmlNode ItemXmlNode in MyXmlNodeList)
        {
            MyXmlNode = ItemXmlNode.SelectSingleNode("amz:ItemAttributes/amz:Title", MyXmlNamespaceManager);
            ResponseMessage += "Title = " + MyXmlNode.InnerText;

            MyXmlNode = ItemXmlNode.SelectSingleNode("amz:ItemAttributes/amz:ListPrice/amz:FormattedPrice", MyXmlNamespaceManager);
            ResponseMessage += "   ListPrice = " + MyXmlNode.InnerText;

            ResponseMessage += Environment.NewLine;
        }
    }

    MessageBox.Show(ResponseMessage); 
}
else
{
    MessageBox.Show(MyREST.ResponseStringFormatted);
}