我想使用Amazon Product Advertising API创建一个带有Powershell的Web请求,该请求提交ISBN / ANSI编号并返回Book Metadata。 (标题,作者,......)
到目前为止,我已经创建了一个帐户来创建AssociateTag,AWSAccessKeyId和AWS Secret Key。通过此信息,我可以在测试页面上创建一个签名的WebRequest,这非常有效。 http://associates-amazon.s3.amazonaws.com/scratchpad/index.html
现在我的问题是,如何使用Powershell创建此WebRequest并使用我的Sec Key签署“String-to-Sign”?
文档http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/rest-signature.html显示,我们需要生成一个HMAC-SHA256哈希密钥,它将添加到WebRequest中。但我无法创建这样的哈希......
**
$AwsSecKey = "1234567890"
$string = "GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&ItemId=0679722769&Operation=I
temLookup&ResponseGroup=ItemAttributes%2COffers%2CImages%2CReview
s&Service=AWSECommerceService&Timestamp=2009-01-01T12%3A00%3A00Z&
Version=2009-01-06"
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($key)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($string))
$sig = [string]::join("", ($signature | % {([int]$_).toString('x2')}))
**
希望有人可以帮我解决这个问题......
谢谢, 安德烈亚斯
答案 0 :(得分:3)
除了latkin的评论之外,您应该使用换行符来构建字符串,否则您的签名将与亚马逊的签名不符。以下是我在Amazon Product Scatchpad处匹配签名的示例。
$amazonserver="webservices.amazon.com"
$linebreak="`n"
$AWSAccessKey = "2345678"
$AssociateTag = "1234567"
$secretkey = "1234567890"
$url = "/onca/xml"
$ItemID = "0679722769"
$timestamp = "2012-10-19T15%3A34%3A33.000Z"
$urlparams = "AWSAccessKeyId=" + $AWSAccessKey + "&AssociateTag=" + $AssociateTag + "&Condition=All&IdType=ASIN&ItemId=" + $ItemID + "&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers&Service=AWSECommerceService&Timestamp=" + $timestamp + "&Version=2011-08-01"
#write-host "Urlparams=$urlparams"
$stringtosign="GET" + $linebreak + $amazonserver + $linebreak + $url + $linebreak + $urlparams
#write-host "stringtosign=$stringtosign"
#Results from Amazon Products Scratchpad, http://associates-amazon.s3.amazonaws.com/scratchpad/index.html
$test="GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=2345678&AssociateTag=1234567&Condition=All&IdType=ASIN&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers&Service=AWSECommerceService&Timestamp=2012-10-19T15%3A34%3A33.000Z&Version=2011-08-01"
#Write-Host $test
$testsig ="9oGX%2Fs8K5ww6CLkAGYLPCiLPAp5kdEBYyBwzOF7fXZI%3D"
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secretkey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringtosign))
#$sig = [string]::join("", ($signature | % {([int]$_).toString('x2')}))
#$sig
$sig = [Convert]::ToBase64String($signature)
$sig
Add-Type -AssemblyName System.Web
$encoded = [System.Web.HttpUtility]::UrlEncode($sig)
$encoded
$testsig -match $encoded
答案 1 :(得分:1)
您是否在AWS网站上看到了Product Advertising API Signed Request Sample Code - C# REST/Query代码?
它包含一个C#SignedRequestHelper
类,可以直接在PowerShell中使用,也可以帮助缩小AWS请求签名的空白。
扎克
答案 2 :(得分:0)
Zach指出的图书馆可能是你最好的选择。但是你走在正确的轨道上,我认为你唯一错误的是要构造最终的哈希字符串,你需要做什么
$sig = [Convert]::ToBase64String($signature)