Search code examples
firebasegoogle-app-enginegoogle-cloud-platformfirebase-realtime-database

Is it possible to integrate app engine and firebase realtime database?


So I am trying to create a web app that is hosted using Google App Engine hosts and has multiple API endpoints. I have a Flask app for running for all the valid endpoints. The website is hosted successfully and it renders basic HTML. The problem is whenever I try getting data from my Firebase real-time database for that specific endpoint, I get an error 500 Internal server error. This is how I am trying to connect to the Firebase database.

    firebase_admin.initialize_app(cred, {
        'databaseURL': f'https://{PROJECT_ID}-default-rtdb.firebaseio.com'
    }}

Whenever I try running the same app locally, on a local host, I can successfully add it to the database, suggesting the Firebase admin is set up correctly and all the keys are correct. I have added my website URL as an authorized domain but this is still now working. I wanted to ask if this is possible and if it is then what would be the best course of action?


Solution

  • You don't need to specify credentials or the URL in initialize_app(), per the documentation for the Firebase Admin SDK:

    default_app = firebase_admin.initialize_app()
    

    Once it is initialized, you can use the Admin SDK to accomplish the following types of tasks:

    • Implement custom authentication
    • Manage your Firebase Authentication users
    • Read and write data from the Realtime Database
    • Send Firebase Cloud Messaging messages

    The default service account for AppEngine should already have the necessary permissions:

    Every call to a Cloud service needs to be authenticated and authorized, including calls from an App Engine app to other Cloud services such as Cloud Storage.

    By default, calls from your App Engine app to services in the same project are authorized.

    If the service account is not authorized then you can grant [email protected] access to Firebase Realtime Database by assigning in IAM the pre-defined role roles/firebase.admin.