Coinbase GDAX NodeJS - 无效的API密钥

时间:2022-09-18 17:09:39

I'm trying to write a script that will cancel all my orders on GDAX. According to the documentation for Cancel an Order I need to send a DELETE request to /delete. But I assume before I can do that I need to sign the message first.

我正在尝试编写一个脚本,取消我在GDAX上的所有订单。根据取消订单的文档,我需要向/删除发送DELETE请求。但我认为在我能做之前我需要先签署消息。

When I submit the request using fetch in Node, I get this response: { message: 'Invalid API Key' }

当我使用节点中的提取提交请求时,我收到此响应:{message:'无效的API密钥'}

Here is the a code sample I am working on, with the confidential stuff replaced of course:

这是我正在处理的代码示例,当然更换了机密内容:

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(){
	getTime(function(time){
		cancelAll(time, function(){
			console.log('done');
		});
	});
}

function getTime(callback){
	fetch.fetchUrl(coinbaseRestAPIURL + '/time', null, function(error, meta, body){
		var response = JSON.parse(body.toString());
		console.log('response', response);

		var timeStamp = response.epoch;
		callback(timeStamp);
	});
}

function cancelAll(timeStamp, callback) {
	// Refer to https://docs.gdax.com/#cancel-an-order

	var signature = getSignature('DELETE', '/delete', "");
	console.log('signature', signature);
	
	var headers = {
		'Content-Type': 'application/json',
		'CB-ACCESS-KEY': coinbaseAPIKey,
		'CB-ACCESS-SIGN': signature,
		'CB-ACCESS-TIMESTAMP': timeStamp, //Date.now() / 1000,
		'CB-ACCESS-PASSPHRASE': coinbasePassword
	};
	console.log('headers', headers);

	fetch.fetchUrl(coinbaseRestAPIURL + '/delete', {
		method: 'DELETE',
		headers: headers
	}, function(error, meta, body){
		var response = JSON.parse(body.toString());
		console.log('response', response);
		callback();
	})
}

function getSignature(method, requestPath, body) {
	// Refer to https://docs.gdax.com/#signing-a-message

	const secret = coinbaseSecret;
	const timestamp = Date.now() / 1000;
	const what = timestamp + method + requestPath + body;
	const key = Buffer(secret, 'base64');
	const hmac = crypto.createHmac('sha256', key);
	const signature = hmac.update(what).digest('base64');

	return signature;
}

start();

2 个解决方案

#1


0  

I got the same reject when using PHP call and tried several combination of the rights/IP whitelist when creating the key, no one works.

我在使用PHP调用时得到了相同的拒绝,并在创建密钥时尝试了几个权限/ IP白名单组合,没有人工作。

However the same code works for the sandbox so I think it might not be a problem in the code, unless, sandbox and live requires different header and that will be quite surprising. The sandbox does not work properly neither, the ticker endpoint returns constantly "Internal server error" at the moment I write this...

但是相同的代码适用于沙箱,所以我认为它可能不是代码中的问题,除非,sandbox和live需要不同的标题,这将是非常令人惊讶的。沙箱也不能正常工作,当我写这个时,自动收报机端点不断返回“内部服务器错误”...

Not yet find a solution.

尚未找到解决方案。

#2


0  

Go to the Gdax-Node Github repo and take a look at their code and examples.

转到Gdax-Node Github repo并查看他们的代码和示例。

1) Create an authenticatedClient by configuring it with your api details, 2) Then simply use the authedClient object and calncelAllOrders method:

1)通过使用api细节配置来创建authenticatedClient,2)然后只需使用authedClient对象和calncelAllOrders方法:

authedClient.cancelAllOrders({product_id: 'BTC-USD'}, callback);

You could wrap this with a function to call 'x' amount of times (it states in the documentation), or you cold think of something fancier if you'd like.

你可以用一个函数来包装它来调用'x'次(它在文档中说明),或者如果你愿意,你可以冷静地想一些更高级的东西。

Note:- make sure you pull the github repo and do not install from npm directly as there are a few bugs and issues that have been fixed on the git repo but NOT pushed to npm.

注意: - 确保你拉github repo并且不要直接从npm安装,因为有一些bug和问题已经在git repo上修复但没有被推到npm。

...so use npm install coinbase/gdax-node when downloading your gdax package.

...所以在下载gdax包时使用npm install coinbase / gdax-node。

Hope that helps a little...

希望有所帮助......

#1


0  

I got the same reject when using PHP call and tried several combination of the rights/IP whitelist when creating the key, no one works.

我在使用PHP调用时得到了相同的拒绝,并在创建密钥时尝试了几个权限/ IP白名单组合,没有人工作。

However the same code works for the sandbox so I think it might not be a problem in the code, unless, sandbox and live requires different header and that will be quite surprising. The sandbox does not work properly neither, the ticker endpoint returns constantly "Internal server error" at the moment I write this...

但是相同的代码适用于沙箱,所以我认为它可能不是代码中的问题,除非,sandbox和live需要不同的标题,这将是非常令人惊讶的。沙箱也不能正常工作,当我写这个时,自动收报机端点不断返回“内部服务器错误”...

Not yet find a solution.

尚未找到解决方案。

#2


0  

Go to the Gdax-Node Github repo and take a look at their code and examples.

转到Gdax-Node Github repo并查看他们的代码和示例。

1) Create an authenticatedClient by configuring it with your api details, 2) Then simply use the authedClient object and calncelAllOrders method:

1)通过使用api细节配置来创建authenticatedClient,2)然后只需使用authedClient对象和calncelAllOrders方法:

authedClient.cancelAllOrders({product_id: 'BTC-USD'}, callback);

You could wrap this with a function to call 'x' amount of times (it states in the documentation), or you cold think of something fancier if you'd like.

你可以用一个函数来包装它来调用'x'次(它在文档中说明),或者如果你愿意,你可以冷静地想一些更高级的东西。

Note:- make sure you pull the github repo and do not install from npm directly as there are a few bugs and issues that have been fixed on the git repo but NOT pushed to npm.

注意: - 确保你拉github repo并且不要直接从npm安装,因为有一些bug和问题已经在git repo上修复但没有被推到npm。

...so use npm install coinbase/gdax-node when downloading your gdax package.

...所以在下载gdax包时使用npm install coinbase / gdax-node。

Hope that helps a little...

希望有所帮助......