Teekeks / pyTwitchAPI

A Python 3.7 compatible implementation of the Twitch API, EventSub, PubSub and Chat
https://pytwitchapi.dev
MIT License
254 stars 38 forks source link

user_auth_refresh_callback throws TypeError in FastAPI Application #238

Closed Braastos closed 1 year ago

Braastos commented 1 year ago

I use the this Lib in a FastAPI Application, which hosts my Stream Overlay. Sometimes when i start the Application i get this Error:


  File "C:\Users\Username\Documents\GitHub\overlay\venv\Lib\site-packages\starlette\routing.py", line 677, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Username\Documents\GitHub\overlay\main.py", line 141, in lifespan
    await twitch.set_user_authentication(twitch_config["user_auth_token"], SCOPES, twitch_config["user_refresh_token"])
  File "C:\Users\Username\Documents\GitHub\overlay\venv\Lib\site-packages\twitchAPI\twitch.py", line 666, in set_user_authentication
    await self.user_auth_refresh_callback(token, refresh_token)
TypeError: object NoneType can't be used in 'await' expression

ERROR:    Application startup failed. Exiting.
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000018F336919D0>

Process finished with exit code 3```

I use Websockets to send Updates to the Overlay.
Startup for the Twitch Connection is handled like its described in the example for [Lifespan Events for FastAPI](https://fastapi.tiangolo.com/advanced/events/)
Not sure if this is an pyTwitchAPI or FastAPI/Starlett problem.

If you need additional information feel free to ask.
Teekeks commented 1 year ago

looks like the callback function you set for Twitch.user_auth_refresh_callback is not a async function.

Braastos commented 1 year ago

You are right. I fell so stupid right now.

But i am still confused why it sometimes works...

Teekeks commented 1 year ago

because this is only an issue, if the token actually got refreshed and the callback is called

Braastos commented 1 year ago

Oh this does make sense