Python Websocket Received error invalid token

Viewed 91

I am trying to get the Livestats from MotoGP using websocket-client in Python.

The problem is that I do not know much about websockets. Judging from the url I think they might be using an Amazon websocket.

I would really like to be able to get the json data that I know this url provides, as the livestats page is not showing what I want, in the way I want it. Also I would like to learn how to do it.

The problem is that I get a invalid token error from the connection. I have the request information from Google Chrome developer tools, so I do not know what is wrong.

It is hard to test as it is a subscription service, and it only really works during a session, but maybe someone can give me a hint of what to try for the next session.

You can request the URL at any time, but I am unsure what would happen if you sent the right request but with a wrong token. I would expect it to respond with unauthorized or something like that.

Here is the python code I am using at the moment:

from websocket import create_connection

token = "1629022556_bfd6797c5a90e11e946777947e3c849dc9e8c0cdbb8db63cbd3f35a2d0b46e90"
origin = 'https://www.motogp.com'
web_key = 'Kd3SkY5Cj6rvA+T11Sk57g=='

headers = {
    'Pragma': 'no-cache',
    'Origin': origin,
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'da,en-US;q=0.9,en;q=0.8',
    'Sec-WebSocket-Key': web_key,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
    'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
    'Cache-Control': 'no-cache',
    'Sec-WebSocket-Version': '13'
}

ws = create_connection("wss://ltjsonweb.amz.motogp.com:2003/", header=headers)

print("Sending 'Hello, World'...")
ws.send("Hello, World")
print("Sent")
print("Receiving...")
result = ws.recv()
print("Received '%s'" % result)
ws.close()

Result

Sending 'Hello, World'...
Sent
Receiving...
Received '{"error":"invalid token."}'

The token and key was altered from original of course. I got both from a running session I have in google chrome.

Chrome Request Image

I hope that someone will be able to help me more forward with this. That would be awesome!

Ref: Websockets-client: https://pypi.org/project/websocket-client/

0 Answers
Related