Forums

Help with Google Cloud ADC (Application Default Credentials) in Django on PythonAnywhere

A screenshot of my app error

Hi everyone,

I'm trying to set up Google Cloud's Application Default Credentials (ADC) for my Django project on PythonAnywhere, but I keep encountering the following error:

Error creating story: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

What I've Done So Far:

  1. Created a Service Account:
  2. I created a service account in Google Cloud and downloaded the JSON key file.
  3. The file is stored at: /home/footageflow/helloworld2003-754c20cfa98d.json.

  4. Set the GOOGLE_APPLICATION_CREDENTIALS Environment Variable:

  5. I added the following line to my .bashrc file: bash export GOOGLE_APPLICATION_CREDENTIALS="/home/footageflow/helloworld2003-754c20cfa98d.json"
  6. I then ran source ~/.bashrc to reload it.

  7. Tried Programmatic Credentials:

  8. I also tried explicitly setting the environment variable in my Django code: python import os os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/home/footageflow/helloworld2003-754c20cfa98d.json"

  9. Attempted CLI Authentication:

  10. On my PythonAnywhere VM, I downloaded and installed the gcloud CLI.
  11. I ran gcloud auth application-default login and authenticated successfully.

Despite all this, the error persists when I try to run my code on PythonAnywhere.

Questions:

  • Is there something I'm missing with the PythonAnywhere environment or how Google Cloud ADC works?
  • Are there specific permissions I need to set for the service account in Google Cloud?
  • Could the issue be related to how PythonAnywhere handles environment variables or service accounts?

Additional Information:

  • My Django project works perfectly locally after authenticating with gcloud.
  • I'm using PythonAnywhere to deploy my Django app.

Any help or guidance would be greatly appreciated!

To make the environment variables available to your web app workers, you should load them in your WSGI file. We have a help page on that here: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/