简体   繁体   English

使用 python 将 Firebase 用户数据导出为 Json 文件格式

[英]Exporting Firebase Users Data to Json file format using python

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.我正在尝试使用 python 将用户 ID、注册日期和他们的电子邮件地址从 firebase 帐户获取到 json 文件,这样我就可以每天运行命令并每天更新用户数据,而不是每天复制/粘贴。 I have never worked with JSON using python ever before.我以前从未使用 python 处理过 JSON。 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.我的老板为我提供了 2 个非常有用的帮助链接,但我不知道如何用 Python 编写确切的代码以使其正常工作。 The links he provided me are as follows:他给我的链接如下:

screenshot of the data I require我需要的数据截图

在此处输入图片说明

This might help you: https://firebase.google.com/docs/auth/admin/manage-users#list_all_users这可能对您有所帮助: 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.您想要做的是重构它并将用户保存到字典中,然后将该字典导出为 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)
``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM