Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Martin91/03efa7e5ef21f6f0e92ebacad684ebe0 to your computer and use it in GitHub Desktop.
Save Martin91/03efa7e5ef21f6f0e92ebacad684ebe0 to your computer and use it in GitHub Desktop.
Postman Pre-request script for Shopee OpenAPI's authentication
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