Non Pro CoinBase API无效签名



我一直试图通过他们的API从非专业CB获得一些数据。

我已经能够从coinbase pro获得数据,使用下面的代码,添加密码短语,但没有骰子与CB…

我一直得到无效签名:(

你知道我错过了什么吗?

const signedMessages = async (timestamp, meth, requestPath) => {
const secret = process.env.cb_all_read_secret;
const method = meth.toUpperCase();
const body = '';
const message = timestamp + method + requestPath + body;
const key = Buffer.from(secret, 'base64');
const hmac = crypto.createHmac('sha256', key);
const cb_access_sign = hmac.update(message).digest('base64');
return cb_access_sign;
};
const listAccounts = async () => {
let timestamp = Math.floor(Date.now() / 1000);
const signed = await signedMessages(timestamp, 'GET', '/v2/accounts');
const url = 'https://api.coinbase.com/v2/accounts';
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'CB-ACCESS-KEY': process.env.cb_all_read_key,
'CB-ACCESS-SIGN': signed,
'CB-ACCESS-TIMESTAMP': timestamp,
},
};
console.log(options);
try {
const data = await fetch(url, options);
const resultat = await data.json();
console.log(resultat);
} catch (error) {
console.log('error: ', error.message);
}
};
listAccounts();

首先我要检查的是你的电脑时钟是最新的,即不是>落后于API服务器30秒。

coinbase文档充其量是令人沮丧的。在一个地方,我看到你正在使用的方法被记录下来。

var what = timestamp + method + requestPath + body;
// decode the base64 secret
var key = Buffer(secret, 'base64');
// create a sha256 hmac with the secret
var hmac = crypto.createHmac('sha256', key);
// sign the require message with the hmac
// and finally base64 encode the result
return hmac.update(what).digest('base64');

当我再次找到它时,我发现了以下文档,它的签名有点不同:

var signature = crypto.createHmac("sha256", apiSecret).update(message).digest("hex");

注意缺少base64缓冲区,摘要是Hex

我修改了你的代码,并能够获得具有查看权限的钱包。我可能会使用所提供的链接中的代码创建一个符号函数,包括您需要包含的任何选项的主体。

const signedMessages = async (timestamp, method, path) => {
const apiSecret = '...';
const bodyStr = '';
let message = timestamp + method.toUpperCase() + '/v2/' + path + bodyStr;
return crypto.createHmac('sha256', apiSecret).update(message).digest('hex');
};
const listAccounts = async () => {
let timestamp = Math.floor(Date.now() / 1000);
const signed = await signedMessages(timestamp, 'GET', 'accounts');
const url = 'https://api.coinbase.com/v2/accounts/';
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'CB-ACCESS-KEY': '...',
'CB-ACCESS-SIGN': signed,
'CB-ACCESS-TIMESTAMP': timestamp,
},
};
console.log(options);
try {
const data = await fetch(url, options);
const resultat = await data.json();
console.log(resultat);
} catch (error) {
console.log('error: ', error.message);
}
};
listAccounts();

但是你猜怎么着…

"deprecated"node api也是这样做的(在我找到文档之前,我就是在那里找到了这个签名方法),它最后一次更新是在4年前。图。

最新更新