Skip to content

Commit

Permalink
Try to refresh invalid google credentials before deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankApiyo committed Oct 17, 2022
1 parent fb6f249 commit d24ee08
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions onadata/libs/utils/api_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import sys
from datetime import datetime

from google.auth.transport.requests import Request
from google.auth.exceptions import RefreshError
from google.oauth2.credentials import Credentials # noqa

from django.conf import settings
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404
Expand All @@ -15,7 +19,6 @@
import six
from celery.backends.rpc import BacklogLimitExceeded
from celery.result import AsyncResult
from google.oauth2.credentials import Credentials # noqa
from kombu.exceptions import OperationalError
from rest_framework import exceptions, status
from rest_framework.response import Response
Expand Down Expand Up @@ -638,9 +641,13 @@ def _get_google_credential(request):
credential = Credentials(token=request.session["access_token"])

if credential and not credential.valid:
# We need the user to provide a new crential
storage.delete()
credential = None
try:
credential.refresh(Request())
storage.credential = credential
storage.save()
except RefreshError:
storage.delete()
credential = None

if not credential:
google_flow = generate_google_web_flow(request)
Expand Down

0 comments on commit d24ee08

Please sign in to comment.