0

I am trying to get the user ids, signup date and their email addresses from the firebase account to json file using python so that I can just run the command everyday and get the users data updated everyday instead of copy/paste everyday. I have never worked with JSON using python ever before. My boss has provided me 2 links for help which are very useful but I don't know how to write the exact code in Python to get it working. The links he provided me are as follows:

screenshot of the data I require

enter image description here

0

1 Answer 1

1

This might help you: https://firebase.google.com/docs/auth/admin/manage-users#list_all_users

from firebase_admin import auth

# Start listing users from the beginning, 1000 at a time.
page = auth.list_users()
while page:
    for user in page.users:
        print('User: ' + user.uid)
    # Get next batch of users.
    page = page.get_next_page()

# Iterate through all users. This will still retrieve users in batches,
# buffering no more than 1000 users in memory at a time.
for user in auth.list_users().iterate_all():
    print('User: ' + user.uid)

The above code is from that link. Want you want to do is refactor it and save the users into a dictionary and then export that dictionary as JSON.

from firebase_admin import auth
import json

users = {}

for user in auth.list_users().iterate_all():
    users[user.id] = user

with open("output.json", "w") as outfile: 
    json.dump(users, outfile)

Or some variation of the above that works to your liking, could be a list of users instead.

from firebase_admin import auth
import json

users = []

for user in auth.list_users().iterate_all():
    users.append(user)

with open("output.json", "w") as outfile: 
    json.dump(users, outfile)
``
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a ton.This was really helpful. If you may can you please also add the arguments to the code (code from the link) to get the identifier(email address) and signin date(date) as shown in the screenshot.
Read the documentation, trust me. Otherwise print(user) and see what stuff it has inside. cough user.email coughcough
i got this error message UserObject has no attribute iterate_all() , Can you help me please !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.