dqk42179 2016-01-09 08:32
浏览 39
已采纳

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

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
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"

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

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

Here's a playground link with all this code.

  • 写回答

1条回答 默认 最新

  • dongre9937 2016-01-09 08:52
    关注

    Looks fine to me, try running:

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

    I added necessary imports and a main function

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果