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

时间:2014-11-26 03:08:29

标签: c# api rest object amazon-web-services

我正在尝试使用C#查询亚马逊的Web服务(AWS)Books API。我在没有运气的情况下阅读Stackoverflow上的相关帖子。我仍然得到错误:“远程服务器返回错误:(403)禁止”。我编码所有+'s和='s。我在这做错了什么?

    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);
    }

1 个答案:

答案 0 :(得分:0)

OP解决方案。

我弄清楚我做错了什么。我必须:用Base64编码HMASHA256哈希,将查询字符串上的缺失字段添加到Amazon的Book API中,并将URLEncoded%XX大写而不将其余字符串大写。下面发布的代码现在可以运行,并且与http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html

上的Amazon文档相匹配。
    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);
    }