Ich arbeite derzeit an meinem Projekt, um einen Python-Wrapper für die Gemini-API (https://docs.gemini.com/rest-api/) zu erstellen. Ich bin gerade dabei, an der Order Events WebSocket (https://docs.gemini.com/websocket-api/#order-events) zu arbeiten und Probleme mit dem Websocket Client für Python zu haben. Dies ist, wie mein Code jetzt:Wie erstellt man einen Websocket-Wrapper mit Python
import json
import hmac
import hashlib
import base64
import time
from websocket import create_connection
import requests
public_key = 'asdfkjdfdfk'
private_key = 'asdfkjdfdfk'
url = "https://api.sandbox.gemini.com/v1/order/status"
def header(method, payload=None):
if payload is None:
payload = {}
payload['request'] = method
payload['nonce'] = int(time.time() * 1000)
payload['order_id'] = '86499545'
b64_payload = base64.b64encode(json.dumps(payload).encode('utf-8'))
signature = hmac.new(private_key.encode('utf-8'), b64_payload, hashlib.sha384).hexdigest()
headers = {
'X-GEMINI-APIKEY': public_key,
'X-GEMINI-PAYLOAD': b64_payload,
'X-GEMINI-SIGNATURE': signature,
}
return headers
r = requests.post(url, headers=header('/v1/order/status'))
print(r.json())
ws = create_connection("wss://api.sandbox.gemini.com/v1/order/events",
header=header('/v1/order/events'))
, wenn ich laufe Skript bekomme ich folgende Fehlermeldung:
{'order_id': '86499545', 'id': '86499545', 'symbol': 'btcusd', 'exchange': 'gemini', 'avg_execution_price': '0.00', 'side': 'buy', 'type': 'exchange limit', 'timestamp': '1511299791', 'timestampms': 1511299791519, 'is_live': False, 'is_cancelled': True, 'is_hidden': False, 'was_forced': False, 'executed_amount': '0', 'remaining_amount': '0.1', 'options': ['maker-or-cancel'], 'price': '7900.00', 'original_amount': '0.1'}
Traceback (most recent call last):
File "C:\Users\uzman\Documents\Python\Gemini- Python API Wrapper\gemini\testing.py", line 35, in <module>
header=header('/v1/status/order'))
File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_core.py", line 214, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_handshake.py", line 63, in handshake
headers, key = _get_handshake_headers(resource, hostname, port, options)
File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_handshake.py", line 110, in _get_handshake_headers
headers.extend(header)
TypeError: sequence item 1: expected str instance, bytes found
[Finished in 1.5s]
Wie Sie von der ersten Anfrage sehen können, die api erfolgreich ermöglicht es uns, einen Auftrag zu erstellen Das bedeutet, dass die Überprüfung korrekt funktioniert. Das Problem scheint jedoch bei dem Modul websocket
zu liegen. Ich kann anscheinend nicht herausfinden, wie man die Web-Socket-Verbindung erfolgreich erstellt. Ich weiß, ich codiert die b64_payload
und signature
mit utf8
Codierung, aber das ist, was das Gemini-api Sie erfordert zu tun und ich kann es nicht ändern, dass, weil, wenn ich tun dann die erste Anforderung nicht