From 4cec0f6540e44cc7d09eca91a660e2c64e55a13e Mon Sep 17 00:00:00 2001 From: nanoric Date: Mon, 15 Oct 2018 05:34:17 -0400 Subject: [PATCH] =?UTF-8?q?[Fix]=20okex:=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=B8=AABUG=EF=BC=9A=E5=9C=A8=E4=BC=A0=E5=8F=82=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E4=B8=8D=E5=BA=94=E4=BC=A0apiSecret?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/okexfuture/vnokexFuture.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/vnpy/api/okexfuture/vnokexFuture.py b/vnpy/api/okexfuture/vnokexFuture.py index 87944aa4..0b3a7e2e 100644 --- a/vnpy/api/okexfuture/vnokexFuture.py +++ b/vnpy/api/okexfuture/vnokexFuture.py @@ -2,13 +2,22 @@ import hashlib import urllib +from vnpy.api.rest import Request, RestClient -######################################################################## -from vnpy.api.rest import RestClient, Request + +#---------------------------------------------------------------------- +def sign(dataWithApiKey, apiSecret): + """ + :param dataWithApiKey: sorted urlencoded args with apiKey + :return: param 'sign' for okex api + """ + dataWithSecret = dataWithApiKey + "&secret_key=" + apiSecret + return hashlib.md5(dataWithSecret.encode()).hexdigest().upper() ######################################################################## class OkexFutureRestBase(RestClient): + host = 'https://www.okex.com/api/v1' #---------------------------------------------------------------------- def __init__(self): @@ -20,11 +29,11 @@ class OkexFutureRestBase(RestClient): # noinspection PyMethodOverriding def init(self, apiKey, apiSecret): # type: (str, str) -> any - super(OkexFutureRestBase, self).init('https://www.okex.com/api/v1') + super(OkexFutureRestBase, self).init(self.host) self.apiKey = apiKey self.apiSecret = apiSecret -#---------------------------------------------------------------------- + #---------------------------------------------------------------------- def beforeRequest(self, req): # type: (Request)->Request args = req.params or {} args.update(req.data or {}) @@ -33,12 +42,9 @@ class OkexFutureRestBase(RestClient): if 'apiKey' not in args: args['api_key'] = self.apiKey data = urllib.urlencode(sorted(args.items())) - data += "&secret_key=" + self.apiSecret - - sign = hashlib.md5(data.encode()).hexdigest().upper() - data += "&sign=" + sign + signature = sign(data, self.apiSecret) + data += "&sign=" + signature req.headers = {'Content-Type': 'application/x-www-form-urlencoded'} req.data = data return req -