当前位置:网站首页>Implementing cos signature with postman

Implementing cos signature with postman

2022-06-24 01:53:00 adanhey

// Get start timestamp

let timestamp =parseInt(new Date().getTime()/1000);

// Get the expiration timestamp

let timestamp2 = timestamp + 3600;

// obtain method

let method = pm.request.method.toString().toLowerCase();

// Get the entire url

let arrUrl = pm.request.url

// adopt url obtain uri

let uuu = arrUrl.toString().split('//')

let start = uuu[1].indexOf("/");

let end = uuu[1].indexOf("?");

if (end==-1){

end = uuu[1].length;

}

let uri = uuu[1].substring(start,end);//stop Omit , Interception from start All characters from beginning to end

// Required parameters for signature

let qAk = pm.globals.get("SecretId");

let SecretKey = pm.globals.get("SecretKey");

// Calculate signature ( Signatures in many projects , The original string also adds the request parameters )

let qSignAlgorithm = 'sha1';

let qSignTime = timestamp + ';' + timestamp2;

let qKeyTime = timestamp + ';' + timestamp2;

let signKey = CryptoJS.HmacSHA1(qKeyTime,SecretKey).toString();

//  Step two : constitute  FormatString

let formatString = [method, uri, '', '', '',].join('\n');

//  Step three : Calculation  StringToSign

let stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(),''].join('\n');

//  Step four : Calculation  Signature

let Signature = CryptoJS.HmacSHA1(stringToSign, signKey).toString();

//  Step five : structure  Authorization

let authorization = [

'q-sign-algorithm=' + qSignAlgorithm,

'q-ak=' + qAk,

'q-sign-time=' + qSignTime,

'q-key-time=' + qKeyTime,

'q-header-list='+'',

'q-url-param-list='+'',

'q-signature=' + Signature

].join('&');

pm.environment.set("Authorization", authorization);

原网站

版权声明
本文为[adanhey]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211110203305007s.html