使用Pyrebase库拒绝Firebase权限

时间:2016-12-11 01:39:14

标签: firebase

我已经设置了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"
}
}

2 个答案:

答案 0 :(得分:9)

我遇到了同样的问题,试图将数据上传到数据库。 我重读了开头: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下载的密钥的路径。

enter image description here

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

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

希望它有所帮助。

答案 1 :(得分:1)

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仪表板并单击“身份验证”选项卡,然后可以在此处添加用户)

相关问题