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

ApiException: Access token is expired / 401 error in long-running scripts #87

Closed
coldenol opened this issue Jun 27, 2024 · 2 comments
Closed

Comments

@coldenol
Copy link

coldenol commented Jun 27, 2024

There is an issue in getting a 401 error (ApiException: Access token is expired) after 9-10 hours of script working.

This is the possible place of the issue:

return token_date > time()

My script is running for such a long intervals because of the API rate limits, and to download call recordings for a day it is running from 7 to 12 hours.

Unfortunately, I have no time to investigate the SDK and just trying to implement such a method in my RingCentralApi class that is being called before each API call:

    def _validate_token(self) -> None:
        """
        Checks if the token is about to expire within 60
        seconds. This is an additional layer of security to already
        existing token expiration checks in the RingCentral SDK.

        """
        expire_time = self.platform.auth().data().get("expire_time", 0)
        if expire_time <= time.time() + 60 or expire_time == 0:
            self.platform.refresh()

UPD:
The workaround with the _validate_token() method didn't help. This time the script failed after 2 hours of running.

SDK version: ringcentral-0.9.0-py3-none-any.whl

@coldenol coldenol changed the title 401 error / ApiException: Access token is expired in long-running scripts ApiException: Access token is expired / 401 error in long-running scripts Jun 27, 2024
@denis-colomiets
Copy link

denis-colomiets commented Jul 1, 2024

So far my script has been running for 60 hours with no http issues. Still watching.

@denis-colomiets
Copy link

This issue can be closed. The problem was solved by wrapping the http request in an infinite while loop that breaks after several failed attempts.

connection_retries = 4
while True:
    connection_retries -= 1
    try:
        response = <>
        return response
    except (ApiException, HTTPError, ConnectionResetError) as e:
        if (
            isinstance(e, ApiException)
            and e.api_response().response().status_code == 401
        ) or (isinstance(e, HTTPError) and e.response.status_code == 401):
            if connection_retries <= 0:
                raise HTTPError("Access token expired.")
            continue

So when getting 401 error it just repeats the request and it it usually successful.

@coldenol coldenol closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants