diff --git a/onadata/libs/utils/api_export_tools.py b/onadata/libs/utils/api_export_tools.py index 1512a3fdce..9aac84d584 100644 --- a/onadata/libs/utils/api_export_tools.py +++ b/onadata/libs/utils/api_export_tools.py @@ -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 @@ -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 @@ -638,9 +641,11 @@ 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()) + except RefreshError: + storage.delete() + credential = None if not credential: google_flow = generate_google_web_flow(request)