如何在 C# 中调用 Amazon 的 Book API

本文关键字:Amazon Book API 调用 | 更新日期: 2023-09-27 18:32:12

我正在尝试用C#查询Amazon的Web Service (AWS) Books API。 我阅读了Stackoverflow上的相关帖子,但没有运气。 我仍然收到错误:"远程服务器返回错误:(403) 禁止"。 我编码了所有的+和=。 请问我在这里做错了什么?

    public const string ACCESSKEY = "xxxxx";  // Replace with Actual Access Key
    public const string SECRETKEY = "zzzzz";  // Replace with Actual Secret Key
    public void ProcessRequest(HttpContext context)
    {
        string ts = DateTime.UtcNow.ToString("s");
        string url = "http://webservices.amazon.com/onca/xml?";
        string req = "AWSAccessKeyId=" + ACCESSKEY + "&Condition=All&IdType=ASIN&ItemId=B00008OE6I&Operation=ItemLookup&ResponseGroup=OfferFull&Service=AWSECommerceService";
        req = req + "&Timestamp=" + URLEncode(ts + "Z").ToUpper();
        string s = "GET'nwebservices.amazon.com'n/onca/xml'n" + req;
        Util.Write(s);
        Util.Write("");
        string hash = HashString(s);
        req = req + "&Signature=" + hash;
        url = url + req;
        Util.Write(url);
        Util.Write("");
        string blob = XGetBlobAtURL(url);
        Util.Write(blob);
    }
    private string URLEncode(string s)
    {
        return(HttpContext.Current.Server.UrlEncode(s));
    }
    private string XGetBlobAtURL(string url, string useragent = "")
    {
        string blob = "";
        try
        {
            WebClient wc = new WebClient();
            if (!Util.IsEmpty(useragent)) { wc.Headers["User-Agent"] = useragent; }
            blob = wc.DownloadString(url);
            wc.Dispose();
        }
        catch(Exception e)
        {
            Util.Write(e.ToString());
        }
        return (blob);
    }
    private string HashString(string s)
    {
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(URLEncode(SECRETKEY));
        HMACSHA256 hmasha256 = new HMACSHA256(keyByte);
        byte[] messageBytes = encoding.GetBytes(s);
        byte[] hashmessage = hmasha256.ComputeHash(messageBytes);
        string s2 = ByteToString(hashmessage); 
        return (URLEncode(s2));
    }
    private string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
        {
            sbinary += buff[i].ToString("X2"); // hex format
        }
        return (sbinary);
    }

如何在 C# 中调用 Amazon 的 Book API

OP 的解决方案。

明白了我做错了什么。我必须:Base64 对HMASHA256哈希进行编码,将查询字符串上缺少的字段添加到亚马逊的 Book API 中,并将 URLEncoding %XX 大写,而不将字符串的其余部分大写。下面发布的代码现在可以工作并与亚马逊的文档匹配:http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

    public const string ACCESSKEY = "xxxxx";
    public const string SECRETKEY = "zzzzz";
    public const string TAG = "ccccc";
    private XmlDocument XML;
    public void ProcessRequest(HttpContext context)
    {
        string ts = DateTime.UtcNow.ToString("s");
        string url = "http://webservices.amazon.com/onca/xml?";
        string req = "AWSAccessKeyId=" + ACCESSKEY + "&AssociateTag=" + TAG;
        // Search by ISBN:
        req = req + "&Condition=All&IdType=ISBN&ItemId=0596004923&Operation=ItemLookup&ResponseGroup=Small&SearchIndex=Books&Service=AWSECommerceService";
        string tsq = "&Timestamp=" + URLEncode(ts + "Z").ToUpper();
        string s = "GET'nwebservices.amazon.com'n/onca/xml'n" + req + tsq;
        string hash = HashString(s);
        req = req + tsq + "&Signature=" + hash;
        url = url + req;
        ReadXML(url);
        WriteXML();
    }
    private void ReadXML(string url)
    {
        XML = new XmlDocument();
        XML.Load(url);
    }
    private void WriteXML()
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "text/xml";
        HttpContext.Current.Response.CacheControl = "no-cache";
        HttpContext.Current.Response.Write(XML.OuterXml);
        HttpContext.Current.Response.End();
    }
    private string UCaseUrlEncode(string s)
    {
        char[] x = HttpUtility.UrlEncode(s).ToCharArray();
        for (int i = 0; i < x.Length - 2; i++)
        {
            if (x[i] == '%')
            {
                x[i+1] = char.ToUpper(x[i+1]);
                x[i+2] = char.ToUpper(x[i+2]);
            }
        }
        return new string(x);
    }
    private string URLEncode(string s)
    {
        return(HttpContext.Current.Server.UrlEncode(s));
    }
    private string URLDecode(string s)
    {
        return (HttpContext.Current.Server.UrlDecode(s));
    }
    private string HashString(string s)
    {
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(SECRETKEY);
        byte[] messageBytes = encoding.GetBytes(s);
        HMACSHA256 hmasha256 = new HMACSHA256(keyByte);
        byte[] hashmessage = hmasha256.ComputeHash(messageBytes);
        return (UCaseUrlEncode(Convert.ToBase64String(hashmessage)));
    }
    private string ByteToString(byte[] buff)
    {
        string sbinary = "";
        for (int i = 0; i < buff.Length; i++)
        {
            sbinary += buff[i].ToString("X2"); // hex format
        }
        return (sbinary);
    }