I used this code: def getrequestparamstring(param): if not param: return "" sb = [] map…
- User
I used this code: def get_request_param_string(param): if not param: return "" sb = [] map = sorted(param.items()) for key, value in map: value = value if value else "" sb.append(key + '=' + urllib.parse.quote(value) + '&') sb.pop() return ''.join(sb)
def url_encode(s): return urllib.parse.quote(s, safe='')
def sign(signVo): if not signVo['requestParam']: signVo['requestParam'] = "" str = signVo['accessKey'] + signVo['reqTime'] + signVo['requestParam'] return actual_signature(str, signVo['secretKey'])
def actual_signature(inputStr, key): hmacSha256 = hmac.new(key.encode(), inputStr.encode(), hashlib.sha256) return hmacSha256.hexdigest()
quantity = 0.5 print(client.order(symbol=symbol,price=200.01, vol=quantity,side=1, type=1, open_type=None, position_id=None, leverage=50, external_oid=None, stop_loss_price=None, take_profit_price=None, position_mode=None, reduce_only=False ))
But terminal returned me: {'success': False, 'code': 602, 'message': 'Signature verification failed!'}
- Assistant
The error message indicates that the signature verification for the request failed. This means that the
sign()function is not generating the correct signature for the request.To fix this issue, you need to ensure that the
actual_signature()function is generating the signature correctly. This function should follow the specific signature generation process specified by the API you are using.Review the API documentation or contact the API provider to verify the correct way to generate the signature. Make sure that you are providing the correct parameters and using the correct encoding.
Once you have confirmed the correct signature generation process, update the
actual_signature()function accordingly.Additionally, ensure that you have the correct values for
signVo['accessKey'],signVo['reqTime'], andsignVo['secretKey']before calling thesign()function. If any of these values are incorrect, the generated signature will also be incorrect. - User
How my code need to seem with your advices ?
- Assistant
Here's an updated version of your code incorporating the suggested changes:
import hmac import hashlib import urllib.parse def get_request_param_string(param): if not param: return "" sb = [] map = sorted(param.items()) for key, value in map: value = value if value else "" sb.append(key + '=' + urllib.parse.quote(value) + '&') sb.pop() return ''.join(sb) def url_encode(s): return urllib.parse.quote(s, safe='') def sign(signVo): if not signVo['requestParam']: signVo['requestParam'] = "" str_to_sign = signVo['accessKey'] + signVo['reqTime'] + signVo['requestParam'] signature = actual_signature(str_to_sign, signVo['secretKey']) return signature def actual_signature(inputStr, key): hmacSha256 = hmac.new(key.encode(), inputStr.encode(), hashlib.sha256) return hmacSha256.hexdigest() quantity = 0.5 # Make sure to provide the correct values for the variables below symbol = "" accessKey = "" reqTime = "" secretKey = "" signVo = { "accessKey" : accessKey, "reqTime" : reqTime, "requestParam" : "", "secretKey" : secretKey } signature = sign(signVo) # Assuming you have a 'client' object for making the request response = client.order(symbol=symbol, price=200.01, vol=quantity, side=1, type=1, open_type=None, position_id=None, leverage=50, external_oid=None, stop_loss_price=None, take_profit_price=None, position_mode=None, reduce_only=False, sign=signature) print(response)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.