skip to Main Content

I have taken the example code from https://docs.woo.org/?shell#authentication and have adapted the code to get the V3 normalised request to use in the signature generation. For endpoints that don’t require any parameters, this works fine, but if parameters are required, I get this error:

"{‘success’: False, ‘code’: -1001, ‘message’: ‘The API signature is invalid.’}"

I have checked my API key and secret and the settings on my account, and everything should be fine.

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())

2

Answers


  1. Chosen as BEST ANSWER

    Customer support came with the following code that seems to work:

    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())
    

  2. It’s hard for me to tell exactly if this will work since I don’t have the actual API credentials, but you should try something like this:

    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"}
    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.dumps(data)}'),
        'Content-Type': 'application/json',
        'Cache-Control': 'no-cache'
    }
    
    
    response = requests.post('https://api.woo.org/v3/algo/order', headers=headers, data=data)
    print(response.json())
    

    The difference is that I am created the signature with the exact data in the dictionary, using json.dumps(data). Let me know if this works for you, and if not, what error messages you see.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search