postman.setGlobalVariable("Timestamp", Date.parse(new Date()) / 1000); postman.setGlobalVariable("Nonce", Date.parse(new Date()) / 1000); //正式环境 //Service = request.url.replace(/https:\/\//, "").replace(/\.tencentcloudapi\.com\//, "") //测试环境 Service = request.url.replace(/https:\/\//, "").replace(/\.test\.ess\.tencent\.cn\//, "") if (request.headers["x-tc-service"]) { Service = request.headers["x-tc-service"] } console.log("Service:"+Service); if (request.method == "GET") { payload = "" CanonicalQueryString = "" }else if(request.method == "POST"){ payload = request.data CanonicalQueryString = "" } var reg = /^http(s)?:\/\/(.*?)\// HashedRequestPayload = CryptoJS.SHA256(payload).toString(CryptoJS.enc.Hex); CanonicalURI = "/"; HTTPRequestMethod = request.method CanonicalHeaders = "content-type:application/json\nhost:" + reg.exec(request.url)[2] + "\n" SignedHeaders = "content-type;host" CanonicalRequest = "" + HTTPRequestMethod + '\n' + CanonicalURI + '\n' + CanonicalQueryString + '\n' + CanonicalHeaders + '\n' + SignedHeaders + '\n' + HashedRequestPayload Algorithm = "TC3-HMAC-SHA256"; RequestTimestamp = postman.getGlobalVariable("Timestamp") Date1 = new Date().toISOString().split('T')[0] CredentialScope = Date1 + "/" + Service + "/" + "tc3_request" HashedCanonicalRequest = CryptoJS.SHA256(CanonicalRequest).toString(CryptoJS.enc.Hex) StringToSign = Algorithm + "\n" + RequestTimestamp + "\n" + CredentialScope + "\n" + HashedCanonicalRequest // SecretKey = "" SecretKey = postman.getGlobalVariable("SecretKey") console.log("SecretKey:"+SecretKey); SecretDate = CryptoJS.HmacSHA256(Date1, "TC3" + SecretKey) SecretService = CryptoJS.HmacSHA256(Service, SecretDate) SecretSigning = CryptoJS.HmacSHA256("tc3_request", SecretService) Signature = CryptoJS.HmacSHA256(StringToSign, SecretSigning).toString(CryptoJS.enc.Hex) postman.setGlobalVariable("Signature", Signature); SecretId = postman.getGlobalVariable("SecretId") console.log("SecretId:"+SecretId); Authorization = Algorithm + ' ' + 'Credential=' + SecretId + '/' + CredentialScope + ', ' + 'SignedHeaders=' + SignedHeaders + ', ' + 'Signature=' + Signature postman.setGlobalVariable("Authorization", Authorization); console.log("Authorization:"+Authorization);
标签:postman,Service,request,电子,SecretKey,腾讯,CryptoJS,Authorization From: https://www.cnblogs.com/cy2268540857/p/16836572.html