英文:
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 && 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);
<!-- 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 && 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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论