[我尝试使用邮递员向Coinbase API进行身份验证时得到“无效签名”

问题描述 投票:0回答:1

这是我的请求前脚本。

// Computes the HMAC for requests sent to the Coinbase Pro API.
//
// - Add the following code as Postman pre-request script
// - Adapt the getPatch function an the variable names according to your needs

const timestamp = Math.floor(Date.now() / 1000);

function getPath(url) {
    // URL path regex works only if your URLs look like this: {{api_url}}/resource
    // If you use hardcoded URLs or any other scheme, adapt the regex pattern!
    const matches = url.match(/.+?(\/.+?)(?:#|\?|$)/);
    return (matches && matches.length > 1) ? matches[1] : ''; 
}
 
function computeSignature(request) {
    const data      = request.data;
    const method    = request.method;
    const path      = getPath(request.url);
    const body      = (method === 'GET' || !data) ? '' : JSON.stringify(data);
    const message   = timestamp + method + path + body;
    const key       = CryptoJS.enc.Base64.parse(pm.variables.get('apiSecret'));
    const hash      = CryptoJS.HmacSHA256(message, key).toString(CryptoJS.enc.Base64);
    
    console.log(message);

    return hash;
}
 
postman.setEnvironmentVariable('hmacSignature', computeSignature(request));
postman.setEnvironmentVariable('hmacTimestamp', timestamp);

这是我的哈希前字符串:1578317285GET / user

我从这里获得了我的请求前脚本:https://www.iopanic.com/post/coinbase_pro_hmac_postman/

有人可以帮助我进行身份验证吗?

谢谢。

postman coinbase-api
1个回答
0
投票

此请求前脚本有效。

// Computes the HMAC for requests sent to the Coinbase Pro API.
//
// - Add the following code as Postman pre-request script
// - Adapt the getPatch function an the variable names according to your needs

const timestamp = Math.floor(Date.now() / 1000);

function getPath(url) {
    // URL path regex works only if your URLs look like this: {{api_url}}/resource
    // If you use hardcoded URLs or any other scheme, adapt the regex pattern!
    const matches = url.match(/.+?(\/.+?)(?:#|\?|$)/);
    return (matches && matches.length > 1) ? matches[1] : ''; 
}
 
function computeSignature(request) {
    const data      = request.data;
    const method    = request.method;
    const path      = getPath(request.url);
    const body      = (method === 'GET' || !data) ? '' : JSON.stringify(data);
    const message   = timestamp + method + path + body;
    const apiSecret = pm.variables.get('apiSecret');
    const hash      = CryptoJS.HmacSHA256(message, apiSecret).toString(CryptoJS.enc.Hex);
    
    console.log(message);
    console.log(hash);

    return hash;
}
 
postman.setEnvironmentVariable('hmacSignature', computeSignature(request));
postman.setEnvironmentVariable('hmacTimestamp', timestamp);
© www.soinside.com 2019 - 2024. All rights reserved.