Python 使用 Firebase Database

2017-06-27  本文已影响1516人  Andrew玩Android

Firebase的Realtime Database,是一个Google推出的轻量级的数据库,简单轻便,但是用于生产环境时斟酌,复杂查询无,数据迁移功能弱

更全面的分析见:不要使用 Firebase 数据库的 10 大理由


吐槽完后还是干正事,换数据库是以后的事但现在必须把当前的事情干完。

因为web端用Python开发,而firebase也支持REST,官方推荐了三个库,我用了其中的python-firebase

1. 认证

 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)

  1. 第一个参数为数据库密钥,获取方式:Firebase -> Setting -> 项目设置 -> 服务账号 -> (旧版凭证)数据库密钥,可以添加密钥后获取。
    gen_secret.png
  2. 第二个参数是注册邮箱
  3. 是否允许debug
  4. 是否允许admin

第三、四参数可以不用设置,默认为False,如果设置为None的话会提示:requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://your_storage.firebaseio.com/***/*.json?auth=token

2. 调用REST

较简单,看文档即可。

上一篇 下一篇

猜你喜欢

热点阅读