gpt4 book ai didi

javascript - Coinbase API 返回 "product not found"以获得有效的产品 ID

转载 作者:行者123 更新时间:2023-12-04 12:56:15 25 4
gpt4 key购买 nike

我目前正在使用沙盒 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

最佳答案

正如 j-petty 提到的,您需要将数据作为 POST 操作的请求正文发送,如 API documentation 中所述。所以这就是你得到“找不到产品”的原因。
这是基于您共享内容的工作代码:

   var crypto = require('crypto');
var request = require('request');

async function cb_request( method, path, headers = {}, body = ''){

var apiKey = 'xxxxxx',
apiSecret = 'xxxxxxx',
apiPass = 'xxxxxxx';

//get unix time in seconds
var timestamp = Math.floor(Date.now() / 1000);

// set the request message
var message = timestamp + method + path + body;
console.log('######## message=' + message);

//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,{
'content-type': 'application/json; charset=UTF-8',
'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,
'body': body

};

return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
console.log(response.statusCode + " " + response.statusMessage);
if (err) reject(err);
resolve(JSON.parse(response.body));
});
});

}

async function main() {

// This queries a product by id (successfully)
try {
console.log('try to call product------->');
console.log( await cb_request('GET','/products/BTC-USD') );
console.log('product------------------->done');
}
catch(e) {
console.log(e);
}

var buyParams = JSON.stringify({
'type': 'market',
'side': 'buy',
'funds': '10',
'product_id': 'BTC-USD'
});

try {
console.log('try to call orders------->');
var buy = await cb_request('POST','/orders', {}, buyParams);
console.log(buy);
console.log('orders----------------------->done');

}
catch(e) {
console.log(e);
}

}

main();
enter image description here

关于javascript - Coinbase API 返回 "product not found"以获得有效的产品 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66959274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com