[Fix] 修正了huobigateway与GET相关的签名方法

This commit is contained in:
nanoric 2018-12-27 01:10:20 -04:00
parent d25065fdea
commit f0c192cbbf
2 changed files with 19 additions and 14 deletions

View File

@ -48,6 +48,7 @@ class Request(object):
statusCode = 'terminated' statusCode = 'terminated'
else: else:
statusCode = self.response.status_code statusCode = self.response.status_code
# todo: encoding error
return ("reuqest : {} {} {} because {}: \n" return ("reuqest : {} {} {} because {}: \n"
"headers: {}\n" "headers: {}\n"
"params: {}\n" "params: {}\n"

View File

@ -19,8 +19,8 @@ from vnpy.api.websocket import WebsocketClient
from vnpy.trader.vtGateway import * from vnpy.trader.vtGateway import *
from vnpy.trader.vtFunction import getTempPath, getJsonPath from vnpy.trader.vtFunction import getTempPath, getJsonPath
#REST_HOST = 'https://api.huobipro.com' REST_HOST = 'https://api.huobipro.com'
REST_HOST = 'https://api.huobi.pro/v1' # REST_HOST = 'https://api.huobi.pro/v1'
WEBSOCKET_MARKET_HOST = 'wss://api.huobi.pro/ws' # Global站行情 WEBSOCKET_MARKET_HOST = 'wss://api.huobi.pro/ws' # Global站行情
WEBSOCKET_TRADE_HOST = 'wss://api.huobi.pro/ws/v1' # 资产和订单 WEBSOCKET_TRADE_HOST = 'wss://api.huobi.pro/ws/v1' # 资产和订单
@ -37,14 +37,21 @@ def _split_url(url):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def createSignature(apiKey, method, host, path, secretKey): def createSignature(apiKey, method, host, path, secretKey, getParams=None):
"""创建签名""" """
sortedParams = ( 创建签名
:param getParams: dict 使用GET方法时附带的额外参数(urlparams)
:return:
"""
sortedParams = [
("AccessKeyId", apiKey), ("AccessKeyId", apiKey),
("SignatureMethod", 'HmacSHA256'), ("SignatureMethod", 'HmacSHA256'),
("SignatureVersion", "2"), ("SignatureVersion", "2"),
("Timestamp", datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')) ("Timestamp", datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'))
) ]
if getParams:
sortedParams.extend(getParams.items())
sortedParams = list(sorted(sortedParams))
encodeParams = urllib.urlencode(sortedParams) encodeParams = urllib.urlencode(sortedParams)
payload = [method, host, path, encodeParams] payload = [method, host, path, encodeParams]
@ -213,16 +220,13 @@ class HuobiRestApi(RestClient):
request.headers = { request.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36" "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"
} }
paramsWithSignature = createSignature(self.apiKey,
additionalParams = createSignature(self.apiKey,
request.method, request.method,
self.signHost, self.signHost,
request.path, request.path,
self.apiSecret) self.apiSecret,
if not request.params: request.params)
request.params = additionalParams request.params = paramsWithSignature
else:
request.params.update(additionalParams)
if request.method == "POST": if request.method == "POST":
request.headers['Content-Type'] = 'application/json' request.headers['Content-Type'] = 'application/json'
@ -500,7 +504,7 @@ class HuobiWebsocketApiBase(WebsocketClient):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onErrorMsg(self, packet): # type: (dict)->None def onErrorMsg(self, packet): # type: (dict)->None
"""""" """"""
self.gateway.writeLog(packet['err-msg'])) self.gateway.writeLog(packet['err-msg'])
######################################################################## ########################################################################