google的firebase restapi的python接口
firebase的Python项目详细描述
谷歌firebase restapi的python接口
作者:joe tilsed创建时间:2019年2月9日;最后更新时间:2019年2月10日
Firebase 3.0.0
谷歌firebase restapi的python接口
安装
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服务。
有关详细信息,请查看每个服务的文档。 以下每个方法都接受用户令牌: 用户的idToken在1小时后过期,因此请确保使用用户的refreshToken以避免过时的令牌。 您还可以使用自定义令牌创建用户,例如: 您还可以提交其他索赔。 然后,您可以将这些令牌发送到客户端以登录,或以服务器上的用户身份登录。 注意:请确保在firebase仪表板的auth->;登录方法下启用了电子邮件/密码提供程序。 您可以使用 要使用唯一的、自动生成的、基于时间戳的键保存数据,请使用 要创建自己的键,请使用 要更新现有条目的数据,请使用 要删除现有条目的数据,请使用 您还可以使用 要对新位置执行多位置写入,我们可以使用 查询返回firebasereponse对象。对这些对象调用 调用 返回一个对象列表,每个对象上都可以调用 要从路径返回数据,只需调用 要仅返回特定路径上的键,请使用 注意: 您可以使用 您至少应该处理 您还可以添加 可以通过将多个查询参数链接在一起来生成查询。 此查询将返回按名称排序的前三个用户。 我们用 此查询将返回按名称排序的用户。 返回具有特定值的数据。 此查询将返回得分为10的用户。 在数据中指定一个范围。 此查询返回按分数排序且分数在3到10之间的用户。 限制返回的数据。 此查询返回按分数排序的前五个用户。 当使用order_by_key()对数据进行排序时,数据将按键升序返回。 当使用order_by_value()时,子项按其值排序。 存储服务允许您将图像上载到FireBase。 就像使用数据库服务一样,您可以使用存储服务构建数据的路径。 put方法采用本地文件的路径和可选的用户令牌。 下载方法采用保存的数据库文件的路径和您希望下载的文件的名称。 get_url方法获取保存的数据库文件的路径并返回存储url。 有关潜在用例,请参阅多位置更新。 有时我们可能需要对数据进行多次排序。例如,我们可能希望检索
特定日期,然后根据喜欢的数量对这些文章进行排序。 目前rest api只允许我们对数据进行一次排序,因此 索引是.firebase.com/docs/security/guide/indexing data.html" rel="nofollow">未对数据库引用启用。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'])
令牌到期
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)
验证电子邮件
0
pip install firebase
发送密码重置电子邮件
1
pip install firebase
获取帐户信息
2
pip install firebase
刷新令牌
3
pip install firebase
数据库
child()
方法构建数据的路径。
4
pip install firebase
保存数据
按
push()
方法。
5
<设置< > >pip install firebase
set()
方法。下面示例中的键是"joe"。
6
pip install firebase
更新
update()
方法。
7
< H4>删除< /H4>pip install firebase
remove()
方法。
8
pip install firebase
多位置更新
update()
方法执行多位置更新。
9
pip install firebase
generate_key()
方法。fromfirebaseimportFirebaseconfig={"apiKey":"apiKey","authDomain":"projectId.firebaseapp.com","databaseURL":"https://databaseName.firebaseio.com","storageBucket":"projectId.appspot.com"}firebase=Firebase(config)
0
检索数据
Val
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
put
和patch
事件。有关详细信息,请参阅"来自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
开始和结束
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
先限制到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键订购
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值排序
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
储存
儿童
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
放置
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
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的密钥生成算法的实现排序
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
常见错误
未定义索引
都是人……
推荐PyPI第三方库