invalid signature

I am trying to place a new order using the Websocket API, but there is always an error message: {“code”:-1022, “msg”: “Signature for this request is not valid.”}.

Can someone help me with this. (I have a Binance Spot Testnet account.)

import uuid
import base64
import json
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from websocket import create_connection
import time

API_KEY = ‘’
PRIVATE_KEY_PATH = ‘test-prv-key.pem’

with open(PRIVATE_KEY_PATH, ‘rb’) as f:
private_key = load_pem_private_key(data=f.read(),password=None)

params = {
‘apiKey’: API_KEY,
‘symbol’: ‘BTCUSDT’,
‘side’: ‘SELL’,
‘type’: ‘LIMIT’,
‘timeInForce’: ‘GTC’,
‘quantity’: ‘1.0000000’,
‘price’: ‘0.20’,
‘timestamp’: int(time.time() * 1000)
}

payload = ‘&’.join([f’{param}={value}’ for param, value in sorted(params.items())])

signature = base64.b64encode(private_key.sign(payload.encode(‘ASCII’)))
params[‘signature’] = signature.decode(‘ASCII’)

request = {
‘id’: str(uuid.uuid4()),
‘method’: ‘order.place’,
‘params’: params
}

ws = create_connection(“wss://testnet.binance.vision/ws-api/v3”)
ws.send(json.dumps(request))
result = ws.recv()
ws.close()

print(result)

Hi, have you tried with this sample code: