Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: object NoneType can't be used in 'await' expression - Async client, token expired #245

Closed
meltdown03 opened this issue Jul 18, 2021 · 1 comment · Fixed by #246 or #247

Comments

@meltdown03
Copy link

meltdown03 commented Jul 18, 2021

Description of Bug
With asyncio=True, and streamer running, gets error when trying to run get_accounts() method and the token has expired and needs to be updated.

Code to Reproduce
Paste in the code that causes the bug to occur.

class TDABot():

    def __init__(self, bot, cog, client_id, ref_url=None):
        self.bot = bot
        self.cog = cog
        self.client_id = client_id
        self.ref_url = ref_url

        if not os.path.exists(TOKEN_PATH):
            self.client = easy_client(self.client_id, self.ref_url, TOKEN_PATH,
                                      webdriver_func=lambda: webdriver.Chrome(
                                          ChromeDriverManager().install()),
                                      asyncio=True)
        else:
            self.client = client_from_token_file(
                TOKEN_PATH, self.client_id, asyncio=True)

    async def get_bp(self):
        print("get_bp command running")
        accts = await self.client.get_accounts()
        acct_info = accts.json()
        bp = acct_info[0]['securitiesAccount']['projectedBalances']['availableFunds']
        return bp

    async def read_stream(self, user, account_id):
        try:
            stream_client = StreamClient(
                self.client, account_id=int(account_id))
            await stream_client.login()
            await user.send(f":white_check_mark: TDA account activity streamer started for account id: {account_id}\n\
         (You can now close this DM.)")
            await stream_client.quality_of_service(StreamClient.QOSLevel.EXPRESS)
            await stream_client.account_activity_sub()
            stream_client.add_account_activity_handler(self.get_activity)

            while True:
                await stream_client.handle_message()

        except UnexpectedResponseCode as e:
            await user.send(f":x: Login failed, error: {e} - RESTART BOT AND TRY AGAIN")

        finally:
            await self.client.close_async_session()

Expected Behavior
Update the token, write the token, return info requested, then continue the program

Actual Behavior
errors out, need to run the command again, but then it works.

Error/Exception Log, If Applicable

Traceback (most recent call last):
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "/home/neal/tda/dcBot.py", line 81, in start
    await self.tda_client.update_game()
  File "/home/neal/tda/tdaBot.py", line 57, in update_game
    bp = await self.get_bp()
  File "/home/neal/tda/tdaBot.py", line 44, in get_bp
    accts = await self.client.get_accounts()
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/tda/client/asynchronous.py", line 21, in _get_request
    resp = await self.session.get(dest, params=params)
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/httpx/_client.py", line 1661, in get
    return await self.request(
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 90, in request
    await self.ensure_active_token()
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 104, in ensure_active_token
    await self.refresh_token(url, refresh_token=refresh_token)
  File "/home/neal/.local/share/virtualenvs/tda-riI-381G/lib/python3.9/site-packages/authlib/integrations/httpx_client/oauth2_client.py", line 149, in _refresh_token
    await self.update_token(self.token, refresh_token=refresh_token)
TypeError: object NoneType can't be used in 'await' expression

Possible causes:
(https://github.com/lepture/authlib/blob/f17395323555de638eceecf51b535da5b91fcb0a/authlib/integrations/httpx_client/oauth2_client.py#L162)
calls this function of the library when the token is updated:
(https://github.com/alexgolec/tda-api/blob/b640f1f677249d8f127b7cf2de13dc55216c410e/tda/auth.py#L26:L32)

@alexgolec
Copy link
Owner

Reopening because it turns out that submitting code without tests is a recipe for buggy code...

@alexgolec alexgolec reopened this Jul 20, 2021
@alexgolec alexgolec linked a pull request Jul 20, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants