Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Initialize Firebase Admin SDK in Cloud Function (Python)

I found a similar question on here, but it was for JavaScript and I am using Python. I'm trying to use the Firebase-Admin SDK so I can verify Firebase Authentication id_tokens in my cloud function. In my requirements.txt I have included firebase-admin, and my main.py file looks like this:

from firebase_admin import auth
from firebase_admin import credentials
def returnSQLresponse(request):
    default_app = firebase_admin.initialize_app()
    headers = request.headers
    if headers and 'Authorization' in headers:
        id_token = headers['Authorization']
    decoded_token = auth.verify_id_token(id_token)
    uid = decoded_token['uid'] 

There are probably other problems with the above code, but my main issue is I am getting the error "in returnSQLresponse default_app = firebase_admin.initialize_app() NameError: name 'firebase_admin' is not defined". How do I initialize the Firebase Admin SDK in Python so I can verify this token? I tried following the guide here: https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_the_firebase_admin_sdk. This guide lead me to where I am at now.

like image 700
Connor Williams Avatar asked Jun 30 '26 07:06

Connor Williams


1 Answers

Notice that your are importing the auth and credentials modules only and failing to import the firebase_admin module itself and therefore you get the:

NameError: name 'firebase_admin' is not defined

when trying to initialize the app by calling:

...
default_app = firebase_admin.initialize_app()
...

Making sure that firebase-admin is added within your requirements.txt file and making the imports in the following way:

import firebase_admin
import firebase_admin.auth as auth
import firebase_admin.credentials as credentials


def returnSQLresponse(request):
    default_app = firebase_admin.initialize_app()
    ...

should clear the NameError error message.

like image 112
Daniel Ocando Avatar answered Jul 01 '26 20:07

Daniel Ocando



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!