2

I am currently using the Pyrebase wrapper to get information (such as their email and created date) of all users. I tried looking through the documentation and cross reference it to Pyrebase documentation however i don't seem to get what i'm looking for. Currently i have tried this:

import pyrebase

config={all required information, including path to service account .json file}
firebase=pyrebase.initialize_app(config)
db=firebase.database()
auth=firebase.auth()


extract_user = db.child('users').child('userId').get()

for x in extract_user.each():
    print(x.val())
    
    auth.get_account_info(user[x.val()])

However i still failed, i know im missing something but im not sure what.

Note: I saved the users userID in the database under userId. So i looped through each and every ID to be used in the 'get_account_info'

Any suggestions or ways i can get this to be done?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Master Irfan Elahee
  • 165
  • 1
  • 1
  • 14

2 Answers2

4
from firebase_admin import credentials
from firebase_admin import auth

cred = credentials.Certificate("./key.json")
initialize_app(cred, {'databaseURL' : "your database..."})

page = auth.list_users()
while page:
for user in page.users:
        print("user: ", user.uid)
    page = page.get_next_page()

Then, after you get user id that looks like "F5aQ0kAe41eV2beoasfhaksfjh2323alskjal" you can see the actual email by:

user = auth.get_user("F5aQ0kAe41eV2beoasfhaksfjh2323alskjal")
print("user email: ", user.email)
jsgalarraga
  • 425
  • 6
  • 13
1

The db.child('users').child('userId').get() in your code reads users from the Realtime Database, where they'll only exist if your application added the there explicitly. Adding a user to Firebase Authentication does not automatically also add it to the Realtime Database.

While Pyrebase allows you to initialize it with a service account, it doesn't replicate all administrative functionality of the Firebase Admin SDKs. As far as I can see in Pyrebase's code, Pyrebase's does not implement a way to list users.

Consider using the Firebase Admin SDK, which has a built-in API to list users.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Is it alright to use both pyrebase and the actual firebase admin module? Or would it cause some kind of conflict? – Master Irfan Elahee Jan 02 '21 at 02:21
  • 1
    I'm quite sure Pyrebase just calls the Firebase REST APIs, so I doubt there'll be direct conflicts there. But I'd recommend first trying to implement all functionality on one, before opting to use both. And since you can't get the list of users from Pyrebase, consider if you can get all you need with only the Admin SDK. – Frank van Puffelen Jan 02 '21 at 02:23