美文网首页
Python 使用 Firebase Database

Python 使用 Firebase Database

作者: Andrew玩Android | 来源:发表于2017-06-27 16:05 被阅读1516次

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

较简单,看文档即可。

相关文章

网友评论

      本文标题:Python 使用 Firebase Database

      本文链接:https://www.haomeiwen.com/subject/ofjecxtx.html