pythondjangofirebase-adminpython-dotenv

Python Firebase Admin SDK, Illegal Firebase credential provided


I'm trying initialising Firebase Admin SDK using .env file on a Django app and I get the following error:

Illegal Firebase credential provided. App must be initialized with a valid credential instance

the initialisation code:


from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env.

 firebase_admin.initialize_app({
                "type": os.getenv("TYPE"),
                "project_id": os.getenv("PROJECT_ID"),
                "client_email": os.getenv("CLIENT_EMAIL"),
                "private_key":  os.getenv("PRIVATE_KEY"),
                "client_id":  os.getenv("CLIENT_ID"),
                "private_key_id":  os.getenv("PRIVATE_KEY_ID"),
                "auth_uri":  os.getenv("AUTH_URI"),
                "token_uri":  os.getenv("TOKEN_URI"),
                "auth_provider_x509_cert_url":  os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
                "client_x509_cert_url":  os.getenv("CLIENT_X509_CERT_URL"),
            })


Solution

  • I found the solution that worked for me on a comment on this post:Initializing Firebase Admin via Environment Variables without storing serviceAccount.json

    Basically I stored all the firebase credentials json in one .env variable (it should be in one line) and used it like this:

    
    from dotenv import load_dotenv
    
    load_dotenv()  # take environment variables from .env.
    
    
    firebaseConfig = json.loads(os.getenv("FIREBASE_CREDENTIALS", "{}"))
    
    
    class FCMController:
        def __init__(self):
            if not firebase_admin._apps:  # check if firebase_admin is initialized
                firebase_admin.initialize_app(
                    credential=credentials.Certificate(firebaseConfig))