我正在尝试编写一个脚本来与在线交易所进行通信。
“公共”请求发送至:https://yobit.net/api/3/ https://yobit.net/api/3/
“交易”请求发送至:https://yobit.net/tapi/ https://yobit.net/tapi/
我的公开请求效果很好。然而,我的“私人呼叫”返回 404 错误。
我的密钥 100% 正确。
我目前生成以下 URL:
我是否误解了文档?也许是错误的 URL 结构?
文档链接--->here https://yobit.net/en/api/
每个 Trade API 请求都应该通过身份验证。
通过发送以下 HTTP 标题来完成身份验证:
密钥 - API 密钥,例如:FAF816D16FFDFBD1D46EEF5D5B10D8A2
签名 - 数字签名,POST 参数 (?param0=val0 & ...& nonce=1) 通过 HMAC-SHA512 由密钥签名
后续请求中的参数随机数(最小 1 到最大 2147483646)应超过前一个请求中的参数。
为了使随机数为空,需要生成新密钥。
我的剧本
class yobit(object):
def __init__(self, key, secret):
self.key = key
self.secret = secret
self.public = ['info', 'ticker', 'depth', 'trades']
self.trade = ['activeorders']
def query(self, method, values={}):
if method in self.public:
url = 'https://yobit.net/api/3/'
elif method in self.trade:
url = 'https://yobit.net/tapi/'
else:
return 'You're doing it wrong'
urlString = ''
for i, k in values.iteritems():
urlString += k+'/'
url += method + '/' + urlString
print url
if method not in self.public:
url += '&apikey=' + self.key
url += '&nonce=' + str(int(time.time()))
signature = hmac.new(self.secret, url, hashlib.sha512).hexdigest()
headers = {'apisign': signature}
else:
headers = {}
print url
req = requests.get(url, headers=headers)
response = json.loads(req.text)
return response
####### PUBLIC API
def getinfo(self):
return self.query('info')
def getticker(self, currency):
return self.query('ticker', {'currency': currency})
def getdepth(self, currency):
return self.query('depth', {'currency': currency})
def gettrades(self, currency):
return self.query('trades', {'currency': currency})
##### TRADE API
def getactiveorders(self, pair):
return self.query('activeorders', {'pair': pair})
PHP 中的一个工作示例
我相信这是 PHP 的一个工作示例,不幸的是我无法阅读这种语言。
function yobit_api_query2($method, $req = array())
{
$api_key = '';
$api_secret = '';
$req['method'] = $method;
$req['nonce'] = time();
$post_data = http_build_query($req, '', '&');
$sign = hash_hmac("sha512", $post_data, $api_secret);
$headers = array(
'Sign: '.$sign,
'Key: '.$api_key,
);
$ch = null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; SMART_API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_URL, 'https://yobit.net/tapi/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING , 'gzip');
$res = curl_exec($ch);
if($res === false)
{
$e = curl_error($ch);
debuglog($e);
curl_close($ch);
return null;
}
curl_close($ch);
$result = json_decode($res, true);
if(!$result) debuglog($res);
return $result;
}