requests / requests-oauthlib

OAuthlib support for Python-Requests!
https://requests-oauthlib.readthedocs.org/
ISC License
1.71k stars 421 forks source link

refresh token workflow #516

Open martibosch opened 11 months ago

martibosch commented 11 months ago

Hello! I am using OAuth2Session as follows:

    with open(token_filepath) as src:
        token = json.load(src)

    def token_saver(token):
        with open(token_filepath, "w") as dst:
            json.dump(token, dst)

    session = requests_oauthlib.OAuth2Session(
        client_id,
        token=token,
        auto_refresh_url=REFRESH_TOKEN_URL,
        auto_refresh_kwargs={"client_id": client_id, "client_secret": client_secret},
        token_updater=token_saver,
    )

    response = session.get(
        SOME_URL,
        data=some_dict,
    )
    response.raise_for_status()

It works at first but once the token expires, a 403 error is raised at the last line. The logs revel that the self._client.add_token call in the OAuth2Session.request does not raise any TokenExpiredError, so the token is not updated and then I get the 403 error.

Am I missing something, or is this a peculiarity of the web service and I should thus implement an ad-hoc solution for my use case?

Thank you. Best, Martí

RileyEv commented 5 months ago

Did you ever solve this? I'm seeing exactly the same thing