API-key format invalid with FIX4.4 API

Hello everyone, I’m trying to connect to the FIX4.4 API, but I always get the message “API-key format invalid” when I try to log on. But my api is correct and so is my certificate. I would like the experts to check the code and help me solve this.
My goal is to obtain market data through the FIX API. I’d be grateful if someone could share a working code as well.

import base64
from datetime import datetime, timedelta
import socket
import ssl
import simplefix
import certifi  # Biblioteca para usar certificados confiáveis do sistema
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives.serialization import load_pem_private_key

# Função para gerar o RawData para o Logon
def logon_raw_data(private_key: Ed25519PrivateKey,
                   sender_comp_id: str,
                   target_comp_id: str,
                   msg_seq_num: str,
                   sending_time: str):
    """
    Computes the value of RawData (96) field in Logon<A> message.
    """
    payload = chr(1).join([
        'A',
        sender_comp_id,
        target_comp_id,
        msg_seq_num,
        sending_time,
    ])

    signature = private_key.sign(payload.encode('ASCII'))
    return base64.b64encode(signature).decode('ASCII')
    
# Caminho para o seu arquivo cert.pem
file_path = r'C:\cert.pem'

# Carregar o arquivo cert.pem para obter a chave privada
with open(file_path, 'rb') as f:
    private_key = load_pem_private_key(data=f.read(), password=None)

# Gerar o timestamp atual
sending_time = datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")#[:-3]
sender_comp_id = "50434234" #fake for safety
target_comp_id = "SPOT"
msg_seq_num = 1



# API (exemplo)
api = 'MCowBQSWR2VwAyEAf2dp2234685mlozudI53bJr0BXcS7T/oQJUPAzFLtHg==' #fake for safety

# Gerar o RawData usando a função logon_raw_data
raw_data = logon_raw_data(private_key,
                          sender_comp_id=sender_comp_id,
                          target_comp_id=target_comp_id,
                          msg_seq_num=str(msg_seq_num),
                          sending_time=sending_time)

# Criar a mensagem FIX
message = simplefix.FixMessage()


BEGIN_STRING = "8=FIX.4.4"
message.append_string(BEGIN_STRING, header=True)
message.append_pair(35, 'A', header=True)
message.append_pair(34, msg_seq_num, header=True)
message.append_pair(49, sender_comp_id, header=True)
message.append_pair(52, sending_time, header=True)
message.append_pair(56, 'SPOT', header=True)
message.append_pair(95, len(raw_data))
message.append_pair(96, raw_data)
message.append_pair(98, 0)
message.append_pair(108, 5)
message.append_pair(141, 'Y')
message.append_pair(553, api)
message.append_pair(25035, 1)

# Codificar a mensagem
r = message.encode()
print(r)

# Configuração do servidor
hostname = 'fix-oe.testnet.binance.vision'

# Criar o contexto SSL para a conexão
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

# Usar o certificado CA confiável do sistema via certifi
context.load_verify_locations(certifi.where())
print(certifi.where())

# Carregar o certificado do cliente (se necessário)
#context.load_cert_chain(certfile=r'C:\cert.pem')

# Conectar e enviar a mensagem
with socket.create_connection((hostname, 9000)) as sock:
    with context.wrap_socket(sock, server_hostname=hostname) as ssock:
        print(ssock.version())  # Verificar a versão SSL/TLS
        ssock.write(r)  # Enviar a mensagem
        print(ssock.recv(2048))  # Receber a resposta

Request FIX:
b’8=FIX.4.4\x019=246\x0135=A\x0134=1\x0149=50434234\x0152=20241216-23:09:36.100646\x0156=SPOT\x0195=88\x0196=iJuf6b5RjA2rs7M9dUFLvhtYf6XsEYY7nAEkLchLHgM6YjIDPSKlJ/zP3yRVCXj3CVKieOAVilY2lrY08NbVDw==\x0198=0\x01108=5\x01141=Y\x01553=MCowBQSWR2VwAyEAf2dp2234685mlozudI53bJr0BXcS7T/oQJUPAzFLtHg==\x0125035=1\x0110=165\x01’
C:\Users\userfake\AppData\Local\anaconda3\envs\notebook\Lib\site-packages\certifi\cacert.pem

TLSv1.3 #I dont know wath is this

Response FIX:
b’8=FIX.4.4\x019=0000113\x0135=3\x0149=SPOT\x0156=50434234\x0134=1\x0152=20241216-23:09:35.440969\x0145=1\x01372=A\x01373=8\x0125016=-2014\x0158=API-key format invalid.\x0110=043\x018=FIX.4.4\x019=0000084\x0135=5\x0149=SPOT\x0156=5015906\x0134=2\x0152=20241216-23:09:35.440979\x0158=API-key format invalid.\x0110=000\x01’

Hey,
Does your API-Key have the FIX_API permission?

Yes, I’ve activated all the options related to the FIX API

Are you using an API-key you generated on testnet.binance.vision?

Hello.
The keys I’m using are the ones I generated in the Binance API manager.

I’ve already connected FIX4.4 with Ctrader, but with Binance it’s the first time and I’ve realized that some things are different, so I need to learn more about it