【发布时间】:2021-07-01 16:25:05
【问题描述】:
我目前使用的是沙盒 API,我可以查询产品,包括单个产品,但如果我尝试下订单,我得到的响应是 { message: 'Product not found' }。
这是我的代码:
async function cb_request( method, path, headers = {}, body = ''){
var apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
apiPass = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';
//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);
// set the request message
var message = timestamp + method + path + body;
//create a hexedecimal encoded SHA256 signature of the message
var key = Buffer.from(apiSecret, 'base64');
var signature = crypto.createHmac('sha256', key).update(message).digest('base64');
//create the request options object
var baseUrl = 'https://api-public.sandbox.pro.coinbase.com';
headers = Object.assign({},headers,{
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-PASSPHRASE': apiPass,
'USER-AGENT': 'request'
});
// Logging the headers here to ensure they're sent properly
console.log(headers);
var options = {
baseUrl: baseUrl,
url: path,
method: method,
headers: headers
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});
}
async function main() {
// This queries a product by id (successfully)
try {
console.log( await cb_request('GET','/products/BTC-USD') );
}
catch(e) {
console.log(e);
}
// Trying to place a buy order here (using the same id as above) returns { message: 'Product not found' }
var buyParams = {
'type': 'market',
'side': 'buy',
'funds': '100',
'product_id': 'BTC-USD'
};
try {
var buy = await cb_request('POST','/orders',buyParams);
console.log(buy);
}
catch(e) {
console.log(e);
}
}
main();
我已经尝试在正文中发送参数,它以invalid signature 响应,即使在字符串化时也是如此。我也尝试过使用params shown in the API docs,但它也以product not found 响应。
有什么想法吗? TIA
【问题讨论】:
-
这适用于任何订单类型或具体的市场订单吗?
-
可能是一个奇怪的权限错误。你有“交易”许可吗?
-
另外,如果您在 /products 端点上进行 GET 调用以获取 BTC-USD 的产品 ID?
标签: javascript node.js rest post coinbase-api