Firebase的Realtime Database腺劣,是一個(gè)Google推出的輕量級(jí)的數(shù)據(jù)庫(kù)沛贪,簡(jiǎn)單輕便怕午,但是用于生產(chǎn)環(huán)境時(shí)斟酌废登,復(fù)雜查詢無,數(shù)據(jù)遷移功能弱郁惜。
更全面的分析見:不要使用 Firebase 數(shù)據(jù)庫(kù)的 10 大理由
吐槽完后還是干正事堡距,換數(shù)據(jù)庫(kù)是以后的事但現(xiàn)在必須把當(dāng)前的事情干完。
因?yàn)閣eb端用Python開發(fā)兆蕉,而firebase也支持REST羽戒,官方推薦了三個(gè)庫(kù),我用了其中的python-firebase虎韵。
1. 認(rèn)證
from firebase import firebase
authentication = firebase.FirebaseAuthentication(THIS_IS_MY_SECRET, 'register@gmail.com', True, True)
firebase.authentication = authentication
print(authentication.extra)
user = authentication.get_user()
print(user.firebase_auth_token)
firebase = firebase.FirebaseApplication('https://your_storage.firebaseio.com', authentication=authentication)
result = firebase.get('/user', '0', params={'print': 'pretty'})
print(result)
firebase.FirebaseAuthentication(SECRET, 'register@gmail.com', True, True)
- 第一個(gè)參數(shù)為數(shù)據(jù)庫(kù)密鑰易稠,獲取方式:Firebase -> Setting -> 項(xiàng)目設(shè)置 -> 服務(wù)賬號(hào) -> (舊版憑證)數(shù)據(jù)庫(kù)密鑰,可以添加密鑰后獲取包蓝。
gen_secret.png - 第二個(gè)參數(shù)是注冊(cè)郵箱
- 是否允許debug
- 是否允許admin
第三驶社、四參數(shù)可以不用設(shè)置企量,默認(rèn)為False,如果設(shè)置為None的話會(huì)提示:requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://your_storage.firebaseio.com/***/*.json?auth=token
2. 調(diào)用REST
較簡(jiǎn)單衬吆,看文檔即可梁钾。