I am getting "invalid-signature" when trying to authenticate with the Coinbase API using Postman

huangapple go评论79阅读模式
英文:

I am getting "invalid-signature" when trying to authenticate with the Coinbase API using Postman

问题

这是我的预请求脚本。

// 用于计算发送到Coinbase Pro API的请求的HMAC。
//
// - 将以下代码添加为Postman预请求脚本
// - 根据您的需求调整getPatch函数和变量名称

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

function getPath(url) {
    // 如果您的URL看起来像这样:{{api_url}}/resource,URL路径正则表达式仅起作用。
    // 如果您使用硬编码的URL或任何其他方案,请适应正则表达式模式!
    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/

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

谢谢。

英文:

This is my pre-request script.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// 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 &amp;&amp; matches.length &gt; 1) ? matches[1] : &#39;&#39;; 
}
 
function computeSignature(request) {
    const data      = request.data;
    const method    = request.method;
    const path      = getPath(request.url);
    const body      = (method === &#39;GET&#39; || !data) ? &#39;&#39; : JSON.stringify(data);
    const message   = timestamp + method + path + body;
    const key       = CryptoJS.enc.Base64.parse(pm.variables.get(&#39;apiSecret&#39;));
    const hash      = CryptoJS.HmacSHA256(message, key).toString(CryptoJS.enc.Base64);
    
    console.log(message);

    return hash;
}
 
postman.setEnvironmentVariable(&#39;hmacSignature&#39;, computeSignature(request));
postman.setEnvironmentVariable(&#39;hmacTimestamp&#39;, timestamp);

<!-- end snippet -->

This is my pre-hash string: 1578317285GET/user

I got my pre-request script from here: https://www.iopanic.com/post/coinbase_pro_hmac_postman/

Can someone please help me with getting authenticated?

Thank you.

答案1

得分: 1

这个预请求脚本已经生效。

// 计算发送到Coinbase Pro API的请求的HMAC。
//
// - 将以下代码添加为Postman预请求脚本
// - 根据您的需求调整getPatch函数和变量名称

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

function getPath(url) {
    // 如果您的URL看起来像这样:{{api_url}}/resource,URL路径正则表达式才有效
    // 如果您使用硬编码的URL或任何其他方案,请调整正则表达式模式!
    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);

希望这对你有帮助!

英文:

This pre-request script worked.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// 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 &amp;&amp; matches.length &gt; 1) ? matches[1] : &#39;&#39;; 
}
 
function computeSignature(request) {
    const data      = request.data;
    const method    = request.method;
    const path      = getPath(request.url);
    const body      = (method === &#39;GET&#39; || !data) ? &#39;&#39; : JSON.stringify(data);
    const message   = timestamp + method + path + body;
    const apiSecret = pm.variables.get(&#39;apiSecret&#39;);
    const hash      = CryptoJS.HmacSHA256(message, apiSecret).toString(CryptoJS.enc.Hex);
    
    console.log(message);
    console.log(hash);

    return hash;
}
 
postman.setEnvironmentVariable(&#39;hmacSignature&#39;, computeSignature(request));
postman.setEnvironmentVariable(&#39;hmacTimestamp&#39;, timestamp);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 21:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定