livekit/python-sdks

False System-time breaks the python server client

Closed this issue · 3 comments

Pretty much the repos example, just added the keys directly:

LIVEKIT_API_KEY="..."
LIVEKIT_API_SECRET="..."
LIVEKIT_URL="https://<project-name>-<id>.livekit.cloud"

async def main():
    # will automatically use the LIVEKIT_API_KEY and LIVEKIT_API_SECRET env vars
    lkapi = api.LiveKitAPI(
        url=LIVEKIT_URL,
        api_key=LIVEKIT_API_KEY,
        api_secret=LIVEKIT_API_SECRET
    )
    room_info = await lkapi.room.create_room(
        api.CreateRoomRequest(name="my-room"),
    )
    print(room_info)
    room_list = await lkapi.room.list_rooms(api.ListRoomsRequest())
    print(room_list)
    await lkapi.aclose()


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

I get:

  File "XXXXXXXX/venv/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 1166, in json
    raise ContentTypeError(
aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/plain; charset=utf-8', url=URL('https://<project-name>-<id>.livekit.cloud/twirp/livekit.RoomService/CreateRoom')

I've tried both https://<project-name>-<id>.livekit.cloud and wss://<project-name>-<id>.livekit.cloud

Bot resulting in this error. I can Imagine that I'm just calling the endpoint wrong and it therefore returns text/plain but the docs aren't very clear what to use as url here.

Regardless I'm thinking the text/pain response should maybe be handled or there should be a clear response printed somewhere

I did also just test the livekit-server-sdk for node.
Here the equivalent using https://<project-name>-<id>.livekit.cloud works fine:

import { RoomServiceClient, Room } from 'livekit-server-sdk';
const LIVEKIT_API_KEY = "..."
const LIVEKIT_API_SECRET = "..."
const LIVEKIT_URL = "https://<project-name>-<id>.livekit.cloud"

const livekitHost = LIVEKIT_URL
const roomService = new RoomServiceClient(livekitHost, LIVEKIT_API_KEY, LIVEKIT_API_SECRET);

const opts = {
    name: 'myroom2',
};
roomService.createRoom(opts).then((room) => {
    console.log('room created', room);
});

So I think this must be a bug in the python server sdk, or am I missing something?

I'm looking to use the sdk in a django app as soon as possible :)

Ok checking a little deeper, I can patch the livekit/api/twirp_client.py to se the actual error.

	...
            else:
                # when we have an error, Twirp always encode it in json
                error_data = await resp.text() # await resp.json()
                raise Exception(error_data)
                # raise TwirpError(error_data["code"], error_data["msg"])

Now I can see the actual error:

Exception: invalid token: eyXXXXX.XXX.CCC-XXX, error: go-jose/go-jose/jwt: validation failed, token not valid yet (nbf)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x75a840c25950>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x75a83f814130>, 68656.59302725)]']
connector: <aiohttp.connector.TCPConnector object at 0x75a840c25850>

Weird though as same token and api key with almost identical setup, works in the node client.

Aright after some further digging It turns out the issue is related to my system time.
It seems that the python client used my ( adjusted ) system time, while the nodejs client does not ( what time does that use? )

I saw in this form that that can cause this exact error.

Setting the system time correctly resolves this issue for me ( partially as I'd prefer not to have to ) but I guess I can close this then.

Still having the actual error message printed by the client would have been quite helpful :D