omarryhan/aiogoogle

UserCreds oauth token not refreshed after 1hour

Closed this issue · 1 comments

Hi,

i have an application where i instantiate the aiogoogle client once and use it throughout the app.
But after 1 our, when the token expires requests fail.

import asyncio
from datetime import datetime

from aiogoogle import Aiogoogle
from aiogoogle.auth.creds import ClientCreds
from aiogoogle.auth.creds import UserCreds


async def main() -> None:
    client = Aiogoogle(
        client_creds=ClientCreds(
            client_id="<my_client_id>",
            client_secret="<my_client_secret>",
        ),
        user_creds=UserCreds(
            refresh_token="<my_refresh_token>",
            scopes=[
                "https://www.googleapis.com/auth/calendar",
                "https://www.googleapis.com/auth/drive",
            ],
        ),
    )

    while True:
        async with client as google:
            calendar_v3 = await google.discover("calendar", "v3")

            resp = await google.as_user(
                calendar_v3.events.get(
                    calendarId="my_calendar_id",
                    eventId="my_event_id",
                ),
                full_res=True,
            )
            resp.raise_for_status()

            data = resp.json
            print(f'{datetime.now()} -> {data.get("summary")}')

            await asyncio.sleep(60)


if __name__ == "__main__":
    asyncio.run(main())

The script above runs for one hour and then raises the following:

aiogoogle.excs.AuthError: 



Unauthorized

Content:
{'code': 401,
 'errors': [{'domain': 'global',
             'location': 'Authorization',
             'locationType': 'header',
             'message': 'Invalid Credentials',
             'reason': 'authError'}],
 'message': 'Request had invalid authentication credentials. Expected OAuth 2 '
            'access token, login cookie or other valid authentication '
            'credential. See '
            'https://developers.google.com/identity/sign-in/web/devconsole-project.',
 'status': 'UNAUTHENTICATED'}

Restarting the script just works again until the token expires again.

Hi, thanks for filing the issue. Fixed in v5.6.0. Please reopen if the issue persists