Skip to content

Instantly share code, notes, and snippets.

@moriya9n
Created November 22, 2021 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moriya9n/3b618810b4b499dc17c8f4020ea30c67 to your computer and use it in GitHub Desktop.
Save moriya9n/3b618810b4b499dc17c8f4020ea30c67 to your computer and use it in GitHub Desktop.
Amazon paapi を golang で直接呼び出す時の signature 生成方法
// reqJson に json.Marshal したものが入っているとする
// APIHost = "webservices.amazon.co.jp"
// APIRegion = "us-west-2"
// APITarget = "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems"
// APIService = "ProductAdvertisingAPI"
bodyReader := bytes.NewReader(reqJson)
req, err := http.NewRequest("POST", "https://" + APIHost + "/paapi5/searchitems", bodyReader)
log.Println("request:", req)
req.Header.Add("content-type", "application/json; charset=utf-8")
req.Header.Add("content-encoding", "amz-1.0")
req.Header.Add("x-amz-target", APITarget)
cred := credentials.NewStaticCredentials("自分のID", "キー", "")
signer := signer.NewSigner(cred)
body, err := ioutil.ReadAll(req.Body)
if err != nil {
// エラー処理
}
_, err = signer.Sign(req, bytes.NewReader(body), APIService, APIRegion, time.Now())
if err != nil {
// エラー処理
}
// http.Client で req を送信
@moriya9n
Copy link
Author

9 行目はドキュメントで探せなかったので、curl の出力を参考にした。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment