1
votes

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.

1
ConnorWilliams@ the answer I posted is related to the specific NameError: error message you are receiving. If you have any other questions regarding the use of the verify_id_token error message I'll strongly encourage you to open a new post.Daniel Ocando

1 Answers

3
votes

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.