Is there a more efficient way to deal with Amazon

2020-04-14 07:01发布

I have finally got the Amazon product advertising API to work on my MVC 5 site. I am using the "SignedRequestHelper" class that was provided on one of the downloads from the Amazon site. I have actually got a ref to the Amazon API but I do not seem to be using it at all at present.

What I am using so far is (controller):

    SignedRequestHelper helper = new SignedRequestHelper("myAWSaccessKeyID",
    "mysecretKey", "webservices.amazon.co.uk");


    Dictionary<String, String> items = new Dictionary<String, String>();

    items.Add("Service", "AWSECommerceService");
    items.Add("Operation", "ItemSearch");
    items.Add("AWSAccessKeyId", "myAWSaccessKeyID");
    items.Add("AssociateTag", "myTag");
    items.Add("SearchIndex", SearchIndex);//This is a string value (selectbox)
    items.Add("ResponseGroup", "Images,ItemAttributes,OfferFull,Offers,OfferSummary,Reviews");
    items.Add("Keywords", keyword);//This is a string value

    string requestUrl = helper.Sign(items);

    ViewBag.Stuff = requestUrl;//Just so I could see the whole URL!

    WebRequest request = HttpWebRequest.Create(requestUrl);
    WebResponse response = request.GetResponse();
    XmlDocument doc = new XmlDocument();
    doc.Load(response.GetResponseStream());

    XmlNodeList titleNodes = doc.GetElementsByTagName("Item");

    ViewBag.Titles = titleNodes;

You may notice I partially the copied the style of JAVA code from the scratch pad.

From that point on in the view I just deal with each part as it comes. It is kind of messy and horrid and dealing with switches like this:

foreach (System.Xml.XmlNode item in ViewBag.Titles)
{
    <h3>Item: @count</h3>
    foreach (System.Xml.XmlNode child in item.ChildNodes)
    {
        switch (child.Name)
        {
            case "ASIN":
                <p>ASIN: @child.InnerText</p>
                break;
            case "MediumImage":
                <img src="@child.ChildNodes[0].InnerText" />
                break;
            case "ItemAttributes":
                foreach (System.Xml.XmlNode child1 in child.ChildNodes)
                {
                    if(child1.Name == "Title")
                    {
                        <p>@child1.InnerText</p>
                    }
                }
                break;
        }

    }
    count++;
}

It works and I can use the XML document etc. I just need to know if there is a way to change it so that it is actually using the API part that was given as a reference. I would rather use proper tools than do it with raw XML like this. I had such difficulty connecting with the Amazon documentation that I basically just tried to connect in the JAVA style code on Amazon's scratchpad.

4条回答
冷血范
2楼-- · 2020-04-14 07:06

I believe that I have finally found a way to use the actual Amazon Prod Adv API now. The problem was working out how to sign the request using the latest API (that I had added as a reference). The reference was added in a similar way to the getting started guide even though that was making reference to VS2005. That is obviously 10 years old but I somehow did get it working with a bit of problem solving. I just never got the signing correct so I ended up using that horrid REST bodge (in my original question)!

The post that has helped me now is this one: amazon product advertising api - item lookup request working example

It is the one marked as the answer. It has only 4 up-votes but it is the best thing I have found. I put all the classes into the controller to test it but I will now have to do it properly using models or extension classes. It worked anyway though.

查看更多
神经病院院长
3楼-- · 2020-04-14 07:10

Take a look to AWS SDK for .Net. Also you can find some guides and how to work with it's APIs.


The AWS SDK for .NET includes the following:

  • The current version of the AWS SDK for .NET.
  • All previous major versions of the AWS SDK for .NET.
  • Sample code that demonstrates how to use the AWS SDK for .NET with several AWS services.
查看更多
Root(大扎)
4楼-- · 2020-04-14 07:22

There is a library that is incredibly thorough for dealing with the Amazon Product Advertising API (PAAPI). When you make a request, you can receive a variety of responses, but this library can handle them all! It reads the XML and puts all the information in an object.

I'm working on two MVC 5 sites right now that interact with the PAAPI. I have a separate folder with the files and a couple files I wrote to make requests and process responses by pulling the data I need out of the object created by the library.

I made a C# console app demo, and you can view it here:

https://github.com/zoenberger/AmazonProductAdvertising

I used this for guidance: https://flyingpies.wordpress.com/2009/08/01/17/

However, I ran into a couple errors:

  1. In some instances, large responses require you to modify the MaxReceivedMessageSize and is show on the demo.
  2. I ran into an error with the ImageSets[] in the library. May people have and the fix is here.
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-04-14 07:30

You can use the following nuget Nager.AmazonProductAdvertising package.

PM> Install-Package Nager.AmazonProductAdvertising

Example Controller

public ActionResult ProductSearch(string search)
{
    var authentication = new AmazonAuthentication();
    authentication.AccessKey = "accesskey";
    authentication.SecretKey = "secretkey";

    var wrapper = new AmazonWrapper(authentication, AmazonEndpoint.DE);
    var result = wrapper.Search(search);

    return View(result);
}

Example View

@model Nager.AmazonProductAdvertising.Model.AmazonItemResponse
@{
    ViewBag.Title = "Search";
}

<table class="table">
<tr>
    <th>ASIN</th>
    <th>SalesRank</th>
</tr>
@foreach (var item in Model.Items.Item)
{
    <tr>
    <td>@item.ASIN</td>
    <td>@item.SalesRank</td>
    </tr>
}
</table>
查看更多
登录 后发表回答