【问题标题】:How do I sign AWS API requests?如何签署 AWS API 请求?
【发布时间】:2014-09-26 02:15:25
【问题描述】:

伙计们。

我一直在工作(嗯,刚刚开始)一个 Web 应用程序,它将利用来自 Amazon Web Services 的图书搜索 Web API。

我的问题...是为了使用他们的 API,您必须使用 HMAC SHA 哈希对所有请求进行签名,并在将签名哈希发送给他们时将其附加到您的查询中。

昨晚我写了一些代码,在阅读了我可以得到的文档之后,我似乎使用我应该使用的这种哈希算法不正确地签署了我对 AWS API 的请求。

这是我目前用于访问他们的 API 的代码,为我自己的安全删除了所有密钥:

Meteor.methods

    "isbnSearch": (isbn) ->

        ts = encodeURIComponent(new Date().toISOString())

        reqArray = [
            "Service=AWSCommerceService",
            "Operation=ItemLookup",
            "ResponseGroup=Medium%2CImages",
            "IdType=ISBN",
            "Timestamp="+ts,
            "AWSAccessKeyId=<Access Key Here>",
            "ItemId="+isbn,
            "Version=2014-08-02"
        ]

        req = reqArray.sort().join('&')

        stringToSign = "Get\nwebservices.amazon.com\n/onca/xml\n" + req
        theHash = CryptoJS.HmacSHA256(stringToSign, "<Secret Key Here>").toString()
        request = req + "&Signature=" + theHash
        reqURL = "http://webservices.amazon.com/onca/xml?" + request

        result = HTTP.get reqURL, (e, res) ->

            console.log res

我正在使用 AWS 给我的 AWSAccessKeyId,以及各个地方的密钥,我想我只是在散列错误。

任何帮助将不胜感激!

我从 AWS 得到的回复如下:

<?xml version="1.0"?>
    <ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2014-08-02/">
        <Error>
            <Code>SignatureDoesNotMatch</Code>
            <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message>
        </Error>
        <RequestId>3377bde5-a006-429d-a1f6-8c4faf1b8efc</RequestId>
    </ItemLookupErrorResponse>

【问题讨论】:

  • 我正面临这个错误。可以分享一下解决方法吗?

标签: javascript ajax amazon-web-services coffeescript meteor


【解决方案1】:

根据http://docs.aws.amazon.com/AWSECommerceService/latest/DG/rest-signature.html(第 9 步),您需要对签名中的所有 + 和 = 进行编码。您的代码中似乎没有这样做。

【讨论】:

    【解决方案2】:

    我建议使用现有的 Meteorite aws-sdk 软件包之一:https://atmospherejs.com/?q=aws

    他们增加了对 AWS Node.js 开发工具包的访问权限。

    您还可以添加 NPM Meteorite 包 (http://atmospherejs.com/package/npm) 以访问 AWS Node.js 开发工具包。

    【讨论】:

    • 这也是我的第一个想法,但 SDK 似乎没有涵盖商业 API
    猜你喜欢
    • 2021-06-03
    • 2017-07-10
    • 1970-01-01
    • 2022-06-15
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多