Last active
April 16, 2021 17:06
-
-
Save Martin91/03efa7e5ef21f6f0e92ebacad684ebe0 to your computer and use it in GitHub Desktop.
Postman Pre-request script for Shopee OpenAPI's authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CryptoJS = require("crypto-js"); | |
var Property = require('postman-collection').Property; | |
var now = new Date(); | |
var timestampNow = parseInt(now.getTime() / 1000); | |
pm.collectionVariables.set("timestamp", timestampNow); | |
var host = pm.collectionVariables.get("host"); | |
var path = pm.request.url.getPath(); | |
var requestURL = host + path; | |
// https://github.com/postmanlabs/postman-app-support/issues/5043#issuecomment-414301254 | |
var requestBody = Property.replaceSubstitutions(pm.request.body.raw, pm.collectionVariables.toObject()) | |
var signatureBaseString = requestURL + "|" + requestBody; | |
var secretKey = pm.collectionVariables.get("secret_key"); | |
var hash = CryptoJS.HmacSHA256(signatureBaseString, secretKey).toString(CryptoJS.enc.Hex); | |
console.log("signature is: ", hash); | |
pm.request.headers.add({key: "Authorization", value: hash}); | |
pm.request.headers.add({key: "Content-Type", value: "application/json"}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment