json Python中的Wootrade V3 API身份验证

ikfrs5lh  于 2023-10-21  发布在  Python
关注(0)|答案(2)|浏览(92)

我从www.example.com上获取了示例代码https://docs.woo.org/?shell#authentication,并对代码进行了调整,以获得用于签名生成的V3规范化请求。对于不需要任何参数的端点,这很好,但如果需要参数,我会得到这个错误:
“成功”:False,“code”:-1001,‘消息’:“API签名无效。”}
我已经检查了我的API密钥和密码以及我帐户上的设置,一切都应该很好。

import datetime
import hmac, hashlib, base64
import requests
import json

staging_api_secret_key = 'MY API SECRET'
staging_api_key = 'MY API KEY'

def _generate_signature(data):
  key = staging_api_secret_key#'key' # Defined as a simple string.
  key_bytes= bytes(key , 'utf-8') # Commonly 'latin-1' or 'utf-8'
  data_bytes = bytes(data, 'utf-8') # Assumes `data` is also a string.
  return hmac.new(key_bytes, data_bytes , hashlib.sha256).hexdigest()

milliseconds_since_epoch = round(datetime.datetime.now().timestamp() * 1000)

headers = {
    'x-api-timestamp': str(milliseconds_since_epoch),
    'x-api-key': staging_api_key,
    'x-api-signature': _generate_signature(str(milliseconds_since_epoch)+'POST/v3/algo/order{ "symbol": "SPOT_BTC_USDT",  "side": "BUY",  "reduceOnly": false,  "type": "MARKET",  "quantity": "0.0001",  "algoType": "TRAILING_STOP",  "callbackRate": "0.012"}'),
    'Content-Type': 'application/json',
    'Cache-Control':'no-cache'
}
data = {"symbol": "SPOT_BTC_USDT",  "side": "BUY",  "reduceOnly": False,  "type": "MARKET",  "quantity": "0.0001",  "algoType": "TRAILING_STOP",  "callbackRate": "0.012"}


response = requests.post('https://api.woo.org/v3/algo/order', headers=headers, data=data)
print(response.json())
wvyml7n5

wvyml7n51#

客户支持附带了以下代码,似乎可以工作:

import datetime
import hashlib
from urllib.parse import urlencode
import hmac
import requests
import json
 
API_SECRET = ""
API_KEY = ""
API_ENDPOINT_BASE = ""
 
ts = round(datetime.datetime.now().timestamp() * 1000)
algo_order ={}
algo_order["symbol"] = "SPOT_BTC_USDT"
algo_order["side"] = "BUY"
algo_order["type"] = "MARKET"
algo_order["quantity"] = "1"
algo_order["algoType"] = "STOP"
algo_order["triggerPrice"] = "27500"
json_formatted_str = json.dumps(algo_order, indent=4)
#hashed_sig = hashlib.sha256(API_SECRET.encode('utf-8'))
def _generate_signature(data):
  key = API_SECRET#'key' # Defined as a simple string.
  key_bytes= bytes(key , 'utf-8') # Commonly 'latin-1' or 'utf-8'
  data_bytes = bytes(data, 'utf-8') # Assumes `data` is also a string.
  return hmac.new(key_bytes, data_bytes , hashlib.sha256).hexdigest()
 
sign = str(ts) + "POST/v3/algo/order"+json_formatted_str
api_endpoint = API_ENDPOINT_BASE + "v3/algo/order"
userdata = requests.post(api_endpoint,
                       headers={
                           "x-api-signature": _generate_signature(sign),
                           "x-api-timestamp": str(ts),
                           "x-api-key": API_KEY,
                           'Content-Type': 'application/json',
                       },data=json_formatted_str
)
print(userdata.json())
pprl5pva

pprl5pva2#

由于我没有实际的API凭据,我很难确切地判断这是否有效,但您应该尝试以下操作:

import json
import datetime
import requests
import hmac, hashlib

staging_api_secret_key = 'MY API SECRET'
staging_api_key = 'MY API KEY'

def _generate_signature(data):
    key = staging_api_secret_key  # 'key' # Defined as a simple string.
    key_bytes = bytes(key, 'utf-8')  # Commonly 'latin-1' or 'utf-8'
    data_bytes = bytes(data, 'utf-8')  # Assumes `data` is also a string.
    return hmac.new(key_bytes, data_bytes, hashlib.sha256).hexdigest()

milliseconds_since_epoch = round(datetime.datetime.now().timestamp() * 1000)
data = {"symbol": "SPOT_BTC_USDT", "side": "BUY", "reduceOnly": False, "type": "MARKET", "quantity": "0.0001", "algoType": "TRAILING_STOP", "callbackRate": "0.012"}
json_formatted_string = json.dumps(data, indent=4)
headers = {
    'x-api-timestamp': str(milliseconds_since_epoch),
    'x-api-key': staging_api_key,
    'x-api-signature': _generate_signature(f'{milliseconds_since_epoch}POST/v3/algo/order{json_formatted_string}'),
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache'
}

response = requests.post('https://api.woo.org/v3/algo/order', headers=headers, data=json_formatted_string)
print(response.json())

不同的是,我使用字典中的确切数据创建了签名,使用json.dumps(data)。让我知道这对你是否有效,如果不,你看到了什么错误消息。

相关问题