最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】C#版的AWS的ResponseGroup的ItemAttributes示例代码

Amazon crifan 2429浏览 0评论

【背景】

折腾:

【记录】继续优化C#版的AWS的ItemLookup接口

过程中,想要试试ResponseGroup的ItemAttributes。

 

【折腾过程】

1. 完整代码:

(1)ItemLookupSample.cs

/**********************************************************************************************
 * Copyright 2009 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file 
 * except in compliance with the License. A copy of the License is located at
 *
 *       http://aws.amazon.com/apache2.0/
 *
 * or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under the License. 
 *
 * ********************************************************************************************
 *
 *  Amazon Product Advertising API
 *  Signed Requests Sample Code
 *
 *  API Version: 2009-03-31
 *
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace AmazonProductAdvtApi
{
    class ItemLookupSample
    {
        private const string awsAccessKeyId = "xxx";
        private const string awsSecretKey = "xxx";
        private const string awsAssociateTag = "xxx";
        
        private const string awsDestination = "ecs.amazonaws.com";
        private const string awsApiVersion = "2011-08-02";
        
        //private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2009-03-31";
        //private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/2011-08-01";
        private const string NAMESPACE = "http://webservices.amazon.com/AWSECommerceService/" + awsApiVersion;
        //private const string ITEM_ID   = "0545010225";
        
        private const string ITEM_ID = "B0083PWAPW";
        
        //<Item>
        //    <ASIN>B0083PWAPW</ASIN>
        //    <ParentASIN>B008GGCAVM</ParentASIN>
        //</Item>
        //private const string ITEM_ID = "B008GGCAVM";
        

        public static void Main()
        {
            SignedRequestHelper helper = new SignedRequestHelper(awsAccessKeyId, awsSecretKey, awsAssociateTag, awsDestination);

            /*
             * Here is an ItemLookup example where the request is stored as a dictionary.
             */
            IDictionary<string, string> reqDict = new Dictionary<string, String>();
            reqDict["Service"] = "AWSECommerceService";
            //r1["Version"] = "2009-03-31";
            //r1["Version"] = "2009-03-31";
            reqDict["Version"] = "2011-08-02";
            reqDict["Operation"] = "ItemLookup";
            reqDict["IdType"] = "ASIN";
            reqDict["ItemId"] = ITEM_ID;
            //reqDict["ResponseGroup"] = "Small";
            //reqDict["ResponseGroup"] = "OfferSummary";
            reqDict["ResponseGroup"] = "ItemAttributes";
            //reqDict["ResponseGroup"] = "VariationSummary";
            //reqDict["ResponseGroup"] = "Variations";

            /*
             * The helper supports two forms of requests - dictionary form and query string form.
             */
            String requestUrl;
            String title;

            requestUrl = helper.Sign(reqDict);
            title = FetchTitle(requestUrl);

            System.Console.WriteLine("Method 1: ItemLookup Dictionary form.");
            System.Console.WriteLine("Title is \"" + title + "\"");
            System.Console.WriteLine();
        }

        private static string FetchTitle(string url)
        {
            try
            {
                WebRequest request = HttpWebRequest.Create(url);
                WebResponse response = request.GetResponse();
                XmlDocument doc = new XmlDocument();
                doc.Load(response.GetResponseStream());

                //for debug
                System.Console.WriteLine(doc.InnerXml);

                XmlNodeList errorMessageNodes = doc.GetElementsByTagName("Message", NAMESPACE);
                if (errorMessageNodes != null && errorMessageNodes.Count > 0)
                {
                    String message = errorMessageNodes.Item(0).InnerText;
                    return "Error: " + message + " (but signature worked)";
                }

                XmlNode titleNode = doc.GetElementsByTagName("Title", NAMESPACE).Item(0);
                string title = titleNode.InnerText;
                
                response.Close();

                return title;
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Caught Exception: " + e.Message);
                System.Console.WriteLine("Stack Trace: " + e.StackTrace);
            }
            finally
            {
 
            }

            return null;
        }
    }
}

 

(2)SignedRequestHelper.cs

/**********************************************************************************************
 * Copyright 2009 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file 
 * except in compliance with the License. A copy of the License is located at
 *
 *       http://aws.amazon.com/apache2.0/
 *
 * or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under the License. 
 *
 * ********************************************************************************************
 *
 *  Amazon Product Advertising API
 *  Signed Requests Sample Code
 *
 *  API Version: 2009-03-31
 *
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Security.Cryptography;

namespace AmazonProductAdvtApi
{
    class SignedRequestHelper
    {
        private string endPoint;
        private string akid;
        
        //added by Crifan Li
        //https://affiliate-program.amazon.co.uk/gp/advertising/api/detail/api-changes.html?ie=UTF8&pf_rd_t=501&ref_=amb_link_83388313_2&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_p=&pf_rd_s=assoc-center-1&pf_rd_r=&pf_rd_i=assoc-api-detail-2-v2
        //Product Advertising API Change Details
        //Associate Tag Parameter: Every request made to the API should include a valid Associate Tag. Any request that does not contain a valid Associate Tag will be rejected with an appropriate error message. For details on the Associate Tag parameter, please refer to our Developer guide.
        //-> must add this Associate Tag, otherwise will return:
        //<Error>
        //    <Code>AWS.MissingParameters</Code>
        //    <Message>Your request is missing required parameters. Required parameters include AssociateTag.</Message>
        //</Error>
        private string associateTag;

        private byte[] secret;
        private HMAC signer;

        private const string REQUEST_URI = "/onca/xml";
        private const string REQUEST_METHOD = "GET";

        /*
         * Use this constructor to create the object. The AWS credentials are available on
         * http://aws.amazon.com
         * 
         * The destination is the service end-point for your application:
         *  US: ecs.amazonaws.com
         *  JP: ecs.amazonaws.jp
         *  UK: ecs.amazonaws.co.uk
         *  DE: ecs.amazonaws.de
         *  FR: ecs.amazonaws.fr
         *  CA: ecs.amazonaws.ca
         */
        public SignedRequestHelper(string awsAccessKeyId, string awsSecretKey, string awsAssociateTag, string destination)
        {
            this.endPoint = destination.ToLower();
            this.akid = awsAccessKeyId;
            
            //added by Crifan Li
            this.associateTag = awsAssociateTag;

            this.secret = Encoding.UTF8.GetBytes(awsSecretKey);
            this.signer = new HMACSHA256(this.secret);
        }

        /*
         * Sign a request in the form of a Dictionary of name-value pairs.
         * 
         * This method returns a complete URL to use. Modifying the returned URL
         * in any way invalidates the signature and Amazon will reject the requests.
         */
        public string Sign(IDictionary<string, string> request)
        {
            // Use a SortedDictionary to get the parameters in naturual byte order, as
            // required by AWS.
            ParamComparer pc = new ParamComparer();
            SortedDictionary<string, string> sortedMap = new SortedDictionary<string, string>(request, pc);

            // Add the AWSAccessKeyId and Timestamp to the requests.
            sortedMap["AWSAccessKeyId"] = this.akid;
            
            //added by Crifan Li
            sortedMap["AssociateTag"] = this.associateTag;

            sortedMap["Timestamp"] = this.GetTimestamp();

            // Get the canonical query string
            string canonicalQS = this.ConstructCanonicalQueryString(sortedMap);

            // Derive the bytes needs to be signed.
            StringBuilder builder = new StringBuilder();
            builder.Append(REQUEST_METHOD)
                .Append("\n")
                .Append(this.endPoint)
                .Append("\n")
                .Append(REQUEST_URI)
                .Append("\n")
                .Append(canonicalQS);

            string stringToSign = builder.ToString();
            byte[] toSign = Encoding.UTF8.GetBytes(stringToSign);

            // Compute the signature and convert to Base64.
            byte[] sigBytes = signer.ComputeHash(toSign);
            string signature = Convert.ToBase64String(sigBytes);
            
            // now construct the complete URL and return to caller.
            StringBuilder qsBuilder = new StringBuilder();
            qsBuilder.Append("http://")
                .Append(this.endPoint)
                .Append(REQUEST_URI)
                .Append("?")
                .Append(canonicalQS)
                .Append("&Signature=")
                .Append(this.PercentEncodeRfc3986(signature));

            return qsBuilder.ToString();
        }

        /*
         * Sign a request in the form of a query string.
         * 
         * This method returns a complete URL to use. Modifying the returned URL
         * in any way invalidates the signature and Amazon will reject the requests.
         */
        public string Sign(string queryString)
        {
            IDictionary<string, string> request = this.CreateDictionary(queryString);
            return this.Sign(request);
        }

        /*
         * Current time in IS0 8601 format as required by Amazon
         */
        private string GetTimestamp()
        {
            DateTime currentTime = DateTime.UtcNow;
            string timestamp = currentTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
            return timestamp;
        }

        /*
         * Percent-encode (URL Encode) according to RFC 3986 as required by Amazon.
         * 
         * This is necessary because .NET's HttpUtility.UrlEncode does not encode
         * according to the above standard. Also, .NET returns lower-case encoding
         * by default and Amazon requires upper-case encoding.
         */
        private string PercentEncodeRfc3986(string str)
        {
            str = HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8);
            str = str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29").Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20");

            StringBuilder sbuilder = new StringBuilder(str);
            for (int i = 0; i < sbuilder.Length; i++)
            {
                if (sbuilder[i] == '%')
                {
                    if (Char.IsLetter(sbuilder[i + 1]) || Char.IsLetter(sbuilder[i + 2]))
                    {
                        sbuilder[i + 1] = Char.ToUpper(sbuilder[i + 1]);
                        sbuilder[i + 2] = Char.ToUpper(sbuilder[i + 2]);
                    }
                }
            }
            return sbuilder.ToString();
        }

        /*
         * Convert a query string to corresponding dictionary of name-value pairs.
         */
        private IDictionary<string, string> CreateDictionary(string queryString)
        {
            Dictionary<string, string> map = new Dictionary<string, string>();

            string[] requestParams = queryString.Split('&');

            for (int i = 0; i < requestParams.Length; i++)
            {
                if (requestParams[i].Length < 1)
                {
                    continue;
                }

                char[] sep = { '=' };
                string[] param = requestParams[i].Split(sep, 2);
                for (int j = 0; j < param.Length; j++)
                {
                    param[j] = HttpUtility.UrlDecode(param[j], System.Text.Encoding.UTF8);
                }
                switch (param.Length)
                {
                    case 1:
                        {
                            if (requestParams[i].Length >= 1)
                            {
                                if (requestParams[i].ToCharArray()[0] == '=')
                                {
                                    map[""] = param[0];
                                }
                                else
                                {
                                    map[param[0]] = "";
                                }
                            }
                            break;
                        }
                    case 2:
                        {
                            if (!string.IsNullOrEmpty(param[0]))
                            {
                                map[param[0]] = param[1];
                            }
                        }
                        break;
                }
            }

            return map;
        }

        /*
         * Consttuct the canonical query string from the sorted parameter map.
         */
        private string ConstructCanonicalQueryString(SortedDictionary<string, string> sortedParamMap)
        {
            StringBuilder builder = new StringBuilder();

            if (sortedParamMap.Count == 0)
            {
                builder.Append("");
                return builder.ToString();
            }

            foreach (KeyValuePair<string, string> kvp in sortedParamMap)
            {
                builder.Append(this.PercentEncodeRfc3986(kvp.Key));
                builder.Append("=");
                builder.Append(this.PercentEncodeRfc3986(kvp.Value));
                builder.Append("&");
            }
            string canonicalString = builder.ToString();
            canonicalString = canonicalString.Substring(0, canonicalString.Length - 1);
            return canonicalString;
        }
    }

    /*
     * To help the SortedDictionary order the name-value pairs in the correct way.
     */     
    class ParamComparer : IComparer<string>
    {
        public int Compare(string p1, string p2)
        {
            return string.CompareOrdinal(p1, p2);
        }
    }
}

 

返回xml为:

<?xml version="1.0"?>
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
    <OperationRequest>
        <RequestId>bed2c8a1-92ac-46b4-8404-5657f3393afe</RequestId>
        <Arguments>
            <Argument Name="Operation" Value="ItemLookup"/>
            <Argument Name="Service" Value="AWSECommerceService"/>
            <Argument Name="Signature" Value="AtqddyzWu2icmZOZ/Xx77THVOwmQv6eE+/VTF3xVF7U="/>
            <Argument Name="AssociateTag" Value="xxx"/>
            <Argument Name="Version" Value="2011-08-02"/>
            <Argument Name="ItemId" Value="B0083PWAPW"/>
            <Argument Name="IdType" Value="ASIN"/>
            <Argument Name="AWSAccessKeyId" Value="xxx"/>
            <Argument Name="Timestamp" Value="2013-06-13T09:08:47Z"/>
            <Argument Name="ResponseGroup" Value="ItemAttributes"/>
        </Arguments>
        <RequestProcessingTime>0.0105040000000000</RequestProcessingTime>
    </OperationRequest>
    <Items>
        <Request>
            <IsValid>True</IsValid>
            <ItemLookupRequest>
                <IdType>ASIN</IdType>
                <ItemId>B0083PWAPW</ItemId>
                <ResponseGroup>ItemAttributes</ResponseGroup>
                <VariationPage>All</VariationPage>
            </ItemLookupRequest>
        </Request>
        <Item>
            <ASIN>B0083PWAPW</ASIN>
            <ParentASIN>B008GGCAVM</ParentASIN>
            <DetailPageURL>http://www.amazon.com/Kindle-Fire-HD/dp/B0083PWAPW%3FSubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0083PWAPW</DetailPageURL>
            <ItemLinks>
                <ItemLink>
                    <Description>Technical Details</Description>
                    <URL>http://www.amazon.com/Kindle-Fire-HD/dp/tech-data/B0083PWAPW%3FSubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>Add To Baby Registry</Description>
                    <URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB0083PWAPW%26SubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>Add To Wedding Registry</Description>
                    <URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB0083PWAPW%26SubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>Add To Wishlist</Description>
                    <URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB0083PWAPW%26SubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>Tell A Friend</Description>
                    <URL>http://www.amazon.com/gp/pdp/taf/B0083PWAPW%3FSubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>All Customer Reviews</Description>
                    <URL>http://www.amazon.com/review/product/B0083PWAPW%3FSubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
                <ItemLink>
                    <Description>All Offers</Description>
                    <URL>http://www.amazon.com/gp/offer-listing/B0083PWAPW%3FSubscriptionId%3Dxxx%26tag%3Dxxx%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0083PWAPW</URL>
                </ItemLink>
            </ItemLinks>
            <ItemAttributes>
                <Binding>Electronics</Binding>
                <Brand>Amazon Digital Services Inc.</Brand>
                <CatalogNumberList>
                    <CatalogNumberListElement>53-000406</CatalogNumberListElement>
                </CatalogNumberList>
                <Color>Black</Color>
                <EAN>2609000009136</EAN>
                <EANList>
                    <EANListElement>2609000009136</EANListElement>
                    <EANListElement>2609000005336</EANListElement>
                    <EANListElement>2609000005497</EANListElement>
                    <EANListElement>2609000006364</EANListElement>
                    <EANListElement>2609000005244</EANListElement>
                    <EANListElement>2609000009471</EANListElement>
                    <EANListElement>2609000006326</EANListElement>
                    <EANListElement>2412437002003</EANListElement>
                    <EANListElement>0848719003796</EANListElement>
                    <EANListElement>2097270920006</EANListElement>
                    <EANListElement>2720320491521</EANListElement>
                    <EANListElement>2486103219309</EANListElement>
                    <EANListElement>2609000009327</EANListElement>
                    <EANListElement>2609000006388</EANListElement>
                </EANList>
                <Feature>Stunning 1280x800 HD display with rich color and deep contrast from any viewing angle</Feature>
                <Feature>Exclusive Dolby audio and dual driver stereo speakers for crisp, booming sound without distortion</Feature>
                <Feature>Ultra-fast Wi-Fi- dual-antenna, dual-band Wi-Fi for 35% faster downloads and streaming</Feature>
                <Feature>Over 22 million movies, TV shows, songs, magazines, books, audiobooks, and popular apps and games</Feature>
                <ItemDimensions>
                    <Height Units="hundredths-inches">40</Height>
                    <Length Units="hundredths-inches">760</Length>
                    <Weight Units="hundredths-pounds">87</Weight>
                    <Width Units="hundredths-inches">540</Width>
                </ItemDimensions>
                <Label>Amazon Digital Services, Inc</Label>
                <ListPrice>
                    <Amount>19900</Amount>
                    <CurrencyCode>USD</CurrencyCode>
                    <FormattedPrice>$199.00</FormattedPrice>
                </ListPrice>
                <Manufacturer>Amazon Digital Services, Inc</Manufacturer>
                <Model>53-000406</Model>
                <MPN>06550-611</MPN>
                <PackageDimensions>
                    <Height Units="hundredths-inches">140</Height>
                    <Length Units="hundredths-inches">1020</Length>
                    <Weight Units="hundredths-pounds">130</Weight>
                    <Width Units="hundredths-inches">730</Width>
                </PackageDimensions>
                <PackageQuantity>1</PackageQuantity>
                <PartNumber>06550-611</PartNumber>
                <ProductGroup>Amazon Devices</ProductGroup>
                <ProductTypeName>ABIS_ELECTRONICS</ProductTypeName>
                <Publisher>Amazon Digital Services, Inc</Publisher>
                <ReleaseDate>2012-09-14</ReleaseDate>
                <Studio>Amazon Digital Services, Inc</Studio>
                <Title>Kindle Fire HD 7", Dolby Audio, Dual-Band Wi-Fi, 16 GB - Includes Special Offers</Title>
                <UPC>848719003796</UPC>
                <UPCList>
                    <UPCListElement>848719003796</UPCListElement>
                </UPCList>
            </ItemAttributes>
        </Item>
    </Items>
</ItemLookupResponse>

 

【总结】

ResponseGroup的ItemAttributes,可以获得某个产品的基本信息,还是很丰富的。

 

更多细节可参考:

http://docs.aws.amazon.com/AWSECommerceService/latest/DG/RG_ItemAttributes.html

 

相关参考:

【整理】C#版的AWS的ResponseGroup的Small示例代码

【整理】C#版的AWS的ResponseGroup的OfferSummary示例代码

【整理】C#版的AWS的ResponseGroup的VariationSummary示例代码

【整理】C#版的AWS的ResponseGroup的Variations示例代码

转载请注明:在路上 » 【整理】C#版的AWS的ResponseGroup的ItemAttributes示例代码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.187 seconds, using 22.16MB memory