【问题标题】:Firebase Permission Denied with Pyrebase library使用 Pyrebase 库拒绝 Firebase 权限
【发布时间】:2020-04-06 09:16:35
【问题描述】:

我已经设置了一个 Firebase 帐户和一个数据库。

我已将带有 API 密钥的配置复制到我的 Python 代码中。

我在 Python 3.5 上仍然收到 401 Permission denied 错误

import pyrebase
config = {
"apiKey": "*****",
"authDomain": "***-bot.firebaseapp.com",
"databaseURL": "https://***-bot.firebaseio.com",
"storageBucket": "ebo-bot.appspot.com"
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
data = {"name": "Mortimer 'Morty' Smith"}
db.child("users").child("Morty").set(data)

我的数据库规则设置为:

{
 "rules": {
".read": "auth != null",
".write": "auth != null"
}
}

【问题讨论】:

    标签: firebase


    【解决方案1】:

    我在尝试将数据上传到数据库时遇到了同样的问题。 我重读了开头:https://github.com/thisbejim/Pyrebase/blob/master/README.md

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

    将 serviceAccount 条目添加到配置中,其中包含您可以从 Firebase 下载的密钥的路径。

    要到达那里:设置 > 项目设置 > 服务帐户 > 生成新私钥。

    将该密钥放在某个所需位置,并将该位置放在“serviceAccount”路径中。

    希望对你有帮助。

    【讨论】:

    • 当我们成功登录后,我们认为 serviceAccount 不是必需的,但是......这有效!
    • @Vitor 我建议不要添加 serviceAccount,这会跳过您设置的所有安全规则。相反,传递 user['idToken'] 就像 Jose 在他们的回答中指出的那样。另请参阅github.com/thisbejim/Pyrebase/blob/master/… 了解更多详情
    【解决方案2】:

    set方法缺少用户['idToken'],你忘记认证了,试试:

    import pyrebase
    config = {
    "apiKey": "*****",
    "authDomain": "***-bot.firebaseapp.com",
    "databaseURL": "https://***-bot.firebaseio.com",
    "storageBucket": "ebo-bot.appspot.com"
    }
    
    firebase = pyrebase.initialize_app(config)
    db = firebase.database()
    auth = firebase.auth()
    
    user = auth.sign_in_with_email_and_password("usernamehere@user.com", "passwordhere")
    
    data = {"name": "Mortimer 'Morty' Smith"}
    db.child("users").child("Morty").set(data,user['idToken'])
    

    (您还需要在运行之前创建一个用户,转到您的 firebase 仪表板并单击身份验证选项卡,您可以在那里添加用户)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 2017-12-26
    • 2019-11-15
    • 2020-07-31
    相关资源
    最近更新 更多