google的firebase restapi的python接口

firebase的Python项目详细描述


谷歌firebase restapi的python接口

作者:joe tilsed创建时间:2019年2月9日;最后更新时间:2019年2月10日

Firebase 3.0.0

谷歌firebase restapi的python接口

firebase

安装

pip install firebase

python版本

firebase是为python 3及更高版本编写的,不能与python 2一起正常工作。

将firebase添加到应用程序中

您的google firebase配置数据可以在firebase上找到。

仅用于基于用户的身份验证,我们可以创建以下配置:

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)

您可以选择向我们的 允许您的服务器以管理员身份向firebase进行身份验证并忽略任何安全规则的配置。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)

默认情况下,添加服务帐户将以管理员身份对所有数据库查询进行身份验证,请签出 如何对用户进行身份验证的认证文档。

使用服务

一个firebase应用程序可以使用多个firebase服务。

firebase.auth()认证

firebase.database()-数据库

firebase.storage()-存储

有关详细信息,请查看每个服务的文档。

身份验证

使用电子邮件和密码登录方法将返回用户数据,包括您可以用来遵守安全规则的令牌。

以下每个方法都接受用户令牌:get()push()set()update()remove()stream()

# Get a reference to the auth serviceauth=firebase.auth()# Log the user inuser=auth.sign_in_with_email_and_password(email,password)# Get a reference to the database servicedb=firebase.database()# data to savedata={"name":"Joe Tilsed"}# Pass the user's idToken to the push methodresults=db.child("users").push(data,user['idToken'])

令牌到期

用户的idToken在1小时后过期,因此请确保使用用户的refreshToken以避免过时的令牌。

user = auth.sign_in_with_email_and_password(email, password)

# before the 1 hour expiry:
user = auth.refresh(user['refreshToken'])

# now we have a fresh token
user['idToken']

自定义标记

您还可以使用自定义令牌创建用户,例如:

token = auth.create_custom_token("your_custom_id")

您还可以提交其他索赔。

token_with_additional_claims = auth.create_custom_token("your_custom_id", {"premium_account": True})

然后,您可以将这些令牌发送到客户端以登录,或以服务器上的用户身份登录。

user = auth.sign_in_with_custom_token(token)

管理用户

创建用户
auth.create_user_with_email_and_password(email,password)

注意:请确保在firebase仪表板的auth->;登录方法下启用了电子邮件/密码提供程序。

验证电子邮件

pip install firebase
0

发送密码重置电子邮件
pip install firebase
1

获取帐户信息
pip install firebase
2

刷新令牌

pip install firebase
3

数据库

您可以使用child()方法构建数据的路径。

pip install firebase
4

保存数据

要使用唯一的、自动生成的、基于时间戳的键保存数据,请使用push()方法。

pip install firebase
5 <设置< > >

要创建自己的键,请使用set()方法。下面示例中的键是"joe"。

pip install firebase
6

更新

要更新现有条目的数据,请使用update()方法。

pip install firebase
7 < H4>删除< /H4>

要删除现有条目的数据,请使用remove()方法。

pip install firebase
8

多位置更新

您还可以使用update()方法执行多位置更新。

pip install firebase
9

要对新位置执行多位置写入,我们可以使用generate_key()方法。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
0

检索数据

Val

查询返回firebasereponse对象。对这些对象调用val()将返回查询数据。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
1

密钥< /H4>

调用key()返回查询数据的键。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
2

每个

返回一个对象列表,每个对象上都可以调用val()key()

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
3

获取

要从路径返回数据,只需调用get()方法即可。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
4

要仅返回特定路径上的键,请使用shallow()方法。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
5

注意:shallow()不能与任何复杂查询一起使用。

流媒体

您可以使用stream()方法收听对数据的实时更改。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
6

您至少应该处理putpatch事件。有关详细信息,请参阅"来自rest api的流媒体"。

您还可以添加流id以帮助您在多次运行时识别流:

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
7

关闭流

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
8

复杂查询

可以通过将多个查询参数链接在一起来生成查询。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
9

此查询将返回按名称排序的前三个用户。

按子菜单排序

我们用order_by_child()开始任何复杂的查询

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
0

此查询将返回按名称排序的用户。

等于

返回具有特定值的数据。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
1

此查询将返回得分为10的用户。

开始和结束

在数据中指定一个范围。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
2

此查询返回按分数排序且分数在3到10之间的用户。

先限制到u,最后限制到u

限制返回的数据。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
3

此查询返回按分数排序的前五个用户。

按U键订购

当使用order_by_key()对数据进行排序时,数据将按键升序返回。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
4

按u值排序

当使用order_by_value()时,子项按其值排序。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
5

储存

存储服务允许您将图像上载到FireBase。

儿童

就像使用数据库服务一样,您可以使用存储服务构建数据的路径。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
6

放置

put方法采用本地文件的路径和可选的用户令牌。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
7

下载

下载方法采用保存的数据库文件的路径和您希望下载的文件的名称。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
8

获取URL

get_url方法获取保存的数据库文件的路径并返回存储url。

fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com","serviceAccount":"path/to/serviceAccountCredentials.json"}firebase=Firebase(config)
9

助手方法

生成密钥

db.generate_key()是firebase的密钥生成算法的实现

有关潜在用例,请参阅多位置更新。

排序

有时我们可能需要对数据进行多次排序。例如,我们可能希望检索 特定日期,然后根据喜欢的数量对这些文章进行排序。

目前rest api只允许我们对数据进行一次排序,因此sort()方法弥合了这一差距。

# Get a reference to the auth serviceauth=firebase.auth()# Log the user inuser=auth.sign_in_with_email_and_password(email,password)# Get a reference to the database servicedb=firebase.database()# data to savedata={"name":"Joe Tilsed"}# Pass the user's idToken to the push methodresults=db.child("users").push(data,user['idToken'])
0

常见错误

未定义索引

索引是.firebase.com/docs/security/guide/indexing data.html" rel="nofollow">未对数据库引用启用。

都是人……

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
将小时和分钟添加到特定时间   java计算三维椭圆轨道上的点   java在JXDatePicker中禁用未来日期   尝试从数据库检索密码值时出现java错误   文本区域中的swing格式。JAVA   ColdFusion 10中的java BouncyCastle库   java HBase mapreduce部分未运行   java为什么公共类需要导入到包中而不是自己的包中?   java如何使用Gson为多个JSON对象编写TypeAdapter?   函数式编程Java 8添加元素的方法   java部署TextFairy,但在构建渐变时看到错误   java FindBugs EI_EXPOSE_REP bug是否只关注日期?   http Java URLConnection:如何确定web文件的大小?   jndi从LDAP条目(名称相同)中获取所有值,并将它们存储在带有Java的Lotus Notes中   筛选结果为HBase时的java OutofOrdersCannerExtenception   java Apache POI SXSSFSheet。getRow()返回null假阳性   关于星型模式的java   在Java中生成链表时出现异常