J'essaye d'écrire un script qui annulera toutes mes commandes sur GDAX. Selon the documentation for Cancel an Order je dois envoyer une demande DELETE à/supprimer. Mais je suppose avant que je puisse le faire, je dois d'abord sign the message.Coinbase GDAX NodeJS - clé API invalide
Quand je soumets la demande en utilisant fetch dans le nœud, je reçois cette réponse: {message: « API clé non valide »}
Voici l'un exemple de code je travaille, avec les choses confidentielles remplacé bien sûr:
var crypto = require('crypto');
var fetch = require('fetch');
const coinbaseSecret = 'abc...';
const coinbaseAPIKey = 'abc...';
const coinbasePassword = 'abc...';
const coinbaseRestAPIURL = "https://api-public.sandbox.gdax.com";
function start(){
\t getTime(function(time){
\t \t cancelAll(time, function(){
\t \t \t console.log('done');
\t \t });
\t });
}
function getTime(callback){
\t fetch.fetchUrl(coinbaseRestAPIURL + '/time', null, function(error, meta, body){
\t \t var response = JSON.parse(body.toString());
\t \t console.log('response', response);
\t \t var timeStamp = response.epoch;
\t \t callback(timeStamp);
\t });
}
function cancelAll(timeStamp, callback) {
\t // Refer to https://docs.gdax.com/#cancel-an-order
\t var signature = getSignature('DELETE', '/delete', "");
\t console.log('signature', signature);
\t
\t var headers = {
\t \t 'Content-Type': 'application/json',
\t \t 'CB-ACCESS-KEY': coinbaseAPIKey,
\t \t 'CB-ACCESS-SIGN': signature,
\t \t 'CB-ACCESS-TIMESTAMP': timeStamp, //Date.now()/1000,
\t \t 'CB-ACCESS-PASSPHRASE': coinbasePassword
\t };
\t console.log('headers', headers);
\t fetch.fetchUrl(coinbaseRestAPIURL + '/delete', {
\t \t method: 'DELETE',
\t \t headers: headers
\t }, function(error, meta, body){
\t \t var response = JSON.parse(body.toString());
\t \t console.log('response', response);
\t \t callback();
\t })
}
function getSignature(method, requestPath, body) {
\t // Refer to https://docs.gdax.com/#signing-a-message
\t const secret = coinbaseSecret;
\t const timestamp = Date.now()/1000;
\t const what = timestamp + method + requestPath + body;
\t const key = Buffer(secret, 'base64');
\t const hmac = crypto.createHmac('sha256', key);
\t const signature = hmac.update(what).digest('base64');
\t return signature;
}
start();