无法使用Golang生成Amazon产品API签名。

huangapple go评论88阅读模式
英文:

Cannot generate Amazon product API signature using Golang

问题

帮助。我无法使用亚马逊和Go提供的测试参数获得正确的签名。

我的签名哈希函数如下所示。根据亚马逊文档,我使用SHA-256和Base64编码。

func HashSignature(str string, secret string) string {
  mac := hmac.New(sha256.New, []byte(secret))
  _, err := mac.Write([]byte(str))
  if err != nil { return "" }

  hash := base64.StdEncoding.EncodeToString(mac.Sum(nil))
  hash = url.QueryEscape(hash)
  return hash
}

我的签名测试函数如下所示。我在Ruby代码中使用下面的规范字符串,它生成了正确的预期签名。所以问题似乎出在我的HashSignature()函数的输出上,但我不知道我在那里做错了什么。

func TestAmazonSignature(t *testing.T) {
/* 这是亚马逊文档中的规范字符串,应该生成下面的预期签名
GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&AssociateTag=mytag-20&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2014-08-18T12%3A00%3A00Z&Version=2013-08-01
*/
  SECRET_KEY := "1234567890"
  CANONICAL_STR := "GET\nwebservices.amazon.com\n/onca/xml\nAWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&AssociateTag=mytag-20&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2014-08-18T12%3A00%3A00Z&Version=2013-08-01"

  EXPECTED := "j7bZM0LXZ9eXeZruTqWm2DIvDYVUU3wxPPpp%2BiXxzQc%3D"

  if RESULT := HashSignature(CANONICAL_STR, SECRET_KEY); RESULT != EXPECTED {
    t.Errorf("\nEXPECTED:\n%v\nRESULT:\n%v", EXPECTED, RESULT)
  } else { fmt.Println("TestAmazonSignature: Signature: OK") }
}

这里是一个包含所有这些代码的Playground链接。

英文:

Help. I can't get the right signature using the test parameters provided by Amazon and Go.

My signature hash function is as follows. I use SHA-256 and base64 encoding as per Amazon documentation.

func HashSignature(str string, secret string) string {
  mac := hmac.New(sha256.New, []byte(secret))
  _, err := mac.Write([]byte(str))
  if err != nil {	return "" }

  hash := base64.StdEncoding.EncodeToString(mac.Sum(nil))
  hash = url.QueryEscape(hash)
  return hash
}

My signature test function is as follows. I use the canonical string below in Ruby code and it generates the correct expected signature. So the problem seems to be with the output of my HashSignature() function, but I don't see what I'm doing wrong there.

func TestAmazonSignature(t *testing.T) {
/* here is the canonical string from Amazon documentation which should yield the expected signature below
GET
webservices.amazon.com
/onca/xml
AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&AssociateTag=mytag-20&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2014-08-18T12%3A00%3A00Z&Version=2013-08-01
*/
  SECRET_KEY := "1234567890"
  CANONICAL_STR := "GET\nwebservices.amazon.com\n/onca/xml\nAWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&AssociateTag=mytag-20&ItemId=0679722769&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2014-08-18T12%3A00%3A00Z&Version=2013-08-01"

  EXPECTED := "j7bZM0LXZ9eXeZruTqWm2DIvDYVUU3wxPPpp%2BiXxzQc%3D"

  if RESULT := HashSignature(CANONICAL_STR, SECRET_KEY); RESULT != EXPECTED {
	t.Errorf("\nEXPECTED:\n%v\nRESULT:\n%v", EXPECTED, RESULT)
  } else { fmt.Println("TestAmazonSignature: Signature: OK") }
}

Here's a playground link with all this code.

答案1

得分: 1

这是翻译好的内容:

看起来没问题,尝试运行一下:

https://play.golang.org/p/w0mQAYx2GQ

我添加了必要的导入和一个主函数。

英文:

Looks fine to me, try running:

https://play.golang.org/p/w0mQAYx2GQ

I added necessary imports and a main function

huangapple
  • 本文由 发表于 2016年1月9日 16:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/34691387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定