在尝试向 OKEx API 进行身份验证时,我不断收到无效签名错误,但我无法理解为什么我的签名未通过。另一只眼睛可能有帮助吗?
以下是 OKEx API 文档中的一些上下文:
*---签名消息---
-
OK-ACCESS-SIGN 标头的生成方式如下:创建一个 prehash
时间戳+方法+请求路径+正文的字符串(其中+
代表字符串连接)准备 Secret 签名预哈希
带有 Secret 的字符串,使用 HMAC SHA256 对签名进行编码
Base64 格式示例:sign=CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'GET' + '/users/self/verify', SecretKey))
-
时间戳值与 OK-ACCESS-TIMESTAMP 标头相同,具有纳秒精度。
-
请求方法应该是大写,即 GET 和 POST。
-
requestPath是请求端点的路径。例子:/orders?before=2&limit=30
-
body是指请求体的String。如果没有请求体可以省略
(GET 请求经常出现这种情况)。例子:{"product_id":"BTC-USD-0309","order_id":"377454671037440"}
-
SecretKey是在创建APIKey时生成的。例子:22582BD0CFF14C41EDBF1AB98506286D*
import hmac
import base64
import requests
import json
from Secrets import okex_key
from Secrets import okex_secret
from Secrets import okex_pass
#get time
def get_time():
urltime= 'https://www.okex.com/api/general/v3/time'
response=requests.get(urltime)
time=response.json()
time=time['iso']
return time
# signature
def signature(timestamp, method, request_path, body,secret_key):
if str(body) == '{}' or str(body) == 'None':
body = ''
message = str(timestamp) + str.upper(method) + request_path + str(body)
mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
d = mac.digest()
return base64.b64encode(d)
# set request header
def get_header():
body= {}
request= 'GET'
endpoint= '/api/spot/v3/accounts'
header = dict()
header['CONTENT-TYPE'] = 'application/json'
header['OK-ACCESS-KEY'] = okex_key
header['OK-ACCESS-SIGN'] = signature(get_time(), request, endpoint , body, okex_secret)
header['OK-ACCESS-TIMESTAMP'] = str(get_time())
header['OK-ACCESS-PASSPHRASE'] = okex_pass
return header
url = 'http://www.okex.com/api/spot/v3/accounts'
header = get_header()
response= requests.get(url, headers=header)
response.json()
问题是您在计算值时给出了两个不同的时间值OK-ACCESS-SIGN
and OK-ACCESS-TIMESTAMP
. Put get_time()
到一个变量中并在两个地方使用它。
current_time = get_time()
header['OK-ACCESS-SIGN'] = signature(current_time, request, endpoint , body, okex_secret)
header['OK-ACCESS-TIMESTAMP'] = str(current_time)
另请注意,目前,在get_time
,你询问 OKEx 的服务器现在几点了。但是,如果您的计算机的本地时间相当正确(您可以在https://time.is,例如)您可以避免执行该 HTTP 请求,而仅使用本地时间。
import datetime
def get_time():
now = datetime.datetime.utcnow()
t = now.isoformat("T", "milliseconds")
return t + "Z"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)