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

Token has been expired or revoked #9

Open
xdekasx opened this issue Mar 23, 2022 · 1 comment
Open

Token has been expired or revoked #9

xdekasx opened this issue Mar 23, 2022 · 1 comment

Comments

@xdekasx
Copy link

xdekasx commented Mar 23, 2022

Hi,

First of all, I think this issue is not caused by django-gmailapi-backend but a Google OAuth2, even so, I expose my case for if I'm wrong.

I configured the package with GMAIL_API_CLIENT_ID, GMAIL_API_CLIENT_SECRET and GMAIL_API_REFRESH_TOKEN and everything works fine until today.

Six months ago ( exactly six month ago, for this I think it's not a issue of this package ), I created the credentials from GCP, and suddenly today, without any change, I started to get google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', '{\n "error": "invalid_grant",\n "error_description": "Token has been expired or revoked."\n}')

By now, I fixed this issue by generating again the refresh token by using gmail_oauth2 --generate_oauth2_token ... as the first time I did and replace it for the old one and now seems to work.

I've read that after 6 months, if refresh token is not used, refresh token is expired, so my question is, is the refresh token never used by django-gmailapi-backend?
My understanding is yes, because the access token expires after 1 hour and uses de refresh token to renew it, and almost every day my app is sending mails and I have'nt found no problem until now.

If helps, my OAuth consent screen is of type Internal, I don't know if this can be a reason of the trouble.

For more details, I let the full error traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 136, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 130, in get_response
    response = self._middleware_chain(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 49, in inner
    response = response_for_exception(request, exc)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 115, in response_for_exception
    log_response(
  File "/usr/local/lib/python3.8/site-packages/django/utils/log.py", line 224, in log_response
    getattr(logger, level)(
  File "/usr/local/lib/python3.8/logging/__init__.py", line 1475, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 1589, in _log
    self.handle(record)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 1599, in handle
    self.callHandlers(record)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 1661, in callHandlers
    hdlr.handle(record)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 954, in handle
    self.emit(record)
  File "/usr/local/lib/python3.8/site-packages/django/utils/log.py", line 122, in emit
    self.send_mail(subject, message, fail_silently=True, html_message=html_message)
  File "/usr/local/lib/python3.8/site-packages/django/utils/log.py", line 125, in send_mail
    mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/core/mail/__init__.py", line 104, in mail_admins
    mail.send(fail_silently=fail_silently)
  File "/usr/local/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python3.8/site-packages/gmailapi_backend/mail.py", line 61, in send_messages
    batch.execute()
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/http.py", line 1557, in execute
    self._refresh_and_apply_credentials(request, http)
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/http.py", line 1249, in _refresh_and_apply_credentials
    _auth.refresh_credentials(creds)
  File "/usr/local/lib/python3.8/site-packages/googleapiclient/_auth.py", line 130, in refresh_credentials
    return credentials.refresh(request)
  File "/usr/local/lib/python3.8/site-packages/google/oauth2/credentials.py", line 208, in refresh
    access_token, refresh_token, expiry, grant_response = _client.refresh_grant(
  File "/usr/local/lib/python3.8/site-packages/google/oauth2/_client.py", line 248, in refresh_grant
    response_data = _token_endpoint_request(request, token_uri, body)
  File "/usr/local/lib/python3.8/site-packages/google/oauth2/_client.py", line 124, in _token_endpoint_request
    _handle_error_response(response_body)
  File "/usr/local/lib/python3.8/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response
    raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', '{\n  "error": "invalid_grant",\n  "error_description": "Token has been expired or revoked."\n}')

If somone can bring me some light of this problem I will be very grateful.

Thank you very much in advance,

@hassanaziz0012
Copy link

Hey, I know this is ages old, but did you ever find a solution for this? I'm going through the same issue right now.

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