Forums

Error: No credential to refresh. Automatic Authentication Google API with Django

I am trying to download a file from my Drive in a Django web app. Firstly, I downloaded the client_secrets.json from Google API. Secondly, I used settings.yaml to create de athentication file (credentials.json). Then, according to this topic in the forum, I uploaded the credentials.json to my Django project, and finally, wrote this code (which is called from a view):

def login():
directorioCredenciales='credentials.json'
gauth=GoogleAuth()
gauth.LoadCredentialsFile(directorioCredenciales)
if gauth.access_token_expired:
    gauth.Refresh()
    gauth.SaveCredentialsFile(directorioCredenciales)
else:
    gauth.Authorize()
gauth.CommandLineAuth()

return GoogleDrive(gauth)

Then, I use another part of code to try to download the file from my drive. But it doesnt matter, because the next error is raised before:

No credential to refresh

The error pointed to the line gauth.Refresh in the upper code.

The unexpected thing is that the code works perfectly runnig in the Python console (IDE), but it doesnt work in Django Any idea about how can I solve it?

Maybe this will help?

Hey pafk, thank you for your reply. I tried to generate again the credentials.json, but it didn't work.

Make sure you're using the correct path to the credentials file: https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/