Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Book Search API using Asp.net

How can amazon API be used to search a book using an ISBN number with asp.net?

like image 445
Sultan Saadat Avatar asked Nov 05 '22 05:11

Sultan Saadat


1 Answers

http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl Create a proxy using svcutil.exe for above given url and then this is the Method to GetBookByISBN. AmazonBook is my cutom DTO you have to create you own.

public static AmazonBook GetBookByISBN(string ISBN)
    {
        WebConfigHelper wch = new WebConfigHelper("AWSSettings");
        AmazonBook book = null;
        string AWSAccessKeyId = wch["AccessKey"];
        string AssociateTag = wch["AssociateTag"];
        string AWSSecKey = wch["SecretKey"];

        BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.MaxReceivedMessageSize = int.MaxValue;

        AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
            binding,
            new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

        // add authentication to the ECS client
        client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(AWSAccessKeyId, AWSSecKey));


        ItemSearchRequest request = new ItemSearchRequest();
        request.SearchIndex = "Books";
        request.Power = "ISBN:" + ISBN.Trim();
        request.ResponseGroup = new string[] { "Large" };
        request.Sort = "salesrank";

        ItemSearchRequest[] requests = new ItemSearchRequest[] { request };

        ItemSearch itemSearch = new ItemSearch();
        itemSearch.AWSAccessKeyId = AWSAccessKeyId;
        itemSearch.AssociateTag = AssociateTag;
        itemSearch.Request = requests;


        try
        {
            ItemSearchResponse response = client.ItemSearch(itemSearch);
            Items info = response.Items[0];
            if (info.Item != null)
            {
                Item[] items = info.Item;
                if (items.Length == 1)
                {
                    book = new AmazonBook(items[0]);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return book;


    }

Reagards,

like image 111
Shoaib Shaikh Avatar answered Nov 09 '22 05:11

Shoaib Shaikh